PDA

View Full Version : SQL Selecting table entries depending upon entry in another table



Icy Penguin
September 11th, 2008, 10:56 PM
I've got one table - called 'posts' which has the price and quality of a book someone would be selling, as well as an entry called 'book' - which is the reference to the id of a row in another table ('books'), which contains the title, author, edition, etc..

I'd like to, within a single query, select the price (which would be from 'posts' and the title, author, edition, etc. (from 'books') in one statement).

So, something like this (except I'm getting errors):

SELECT (posts.price, posts.id FROM posts), (books.title, books.price WHERE id = posts.book)

Thanks :)

eirche
September 11th, 2008, 11:22 PM
so it's zero/many posts to one book relationship

sorry for renaming your tables and fields, but i think this is easier to read


post
{
id
price
seller
}

book
{
id
isbn
title
author
}



select `p`.`price`, `p`.`seller`, `b`.`title`
from `post` as `p` left join `book` as `b`
on `p`.`bookid` = `b`.`id`

Icy Penguin
September 11th, 2008, 11:47 PM
You're a genius! Thank you very much!