Select query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
minds_gifts
Forum Commoner
Posts: 63
Joined: Mon Feb 10, 2003 4:23 am

Select query

Post by minds_gifts »

Hello,

I have two tables: Authors(aut_id, aut_name) and books(b_id,aut_id,b_name)

How should i write my query in oder to display the aut_name from the table aut where there are no any book names under this aut_id.

Table: Authors
-----------------
1. Taub
2. Proakis
3. Paul gray

Table: Books
---------------
1. 1. C Techniques
2. 1. Networks
3. 2. DSP

Now when i run this query, i want to show only Paul gray.

Many thanks
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

I think your problem is solved with a JOIN.

Try if this helps you

Code: Select all

SELECT * FROM aut
  LEFT JOIN book ON aut.id = book.aut_id
WHERE aut.name='Paul gray'
Rob the R
Forum Contributor
Posts: 128
Joined: Wed Nov 06, 2002 2:25 pm
Location: Houston

Post by Rob the R »

And if you want to generalize the query so that it lists all authors that have no books associated with them, you can do this:

Code: Select all

SELECT * FROM aut 
LEFT JOIN book ON aut.id = book.aut_id 
WHERE book.aut_id is NULL
Post Reply