views:

25

answers:

2

I keep getting an error saying that a specific column, doesn't exist for b.BookCode But I am fully aware that it does exist! I just wanted to make sure after staring at this for so long that I am not missing something obvious.

SELECT AuthorLast, AuthorFirst, OnHand, Title 
FROM (Inventory i, Author a, Book b) 
WHERE (i.BookCode = b.BookCode AND b.AuthorNum = a.AuthorNum);

I'm very new to SQL so I'm unsure if my syntax is off, Also do I need parentheses around the columns that I mentioned in SELECT. I had parentheses around them at first and got an error and was confused as to why.

Thanks!

A: 

Minimally, you should change this to:

SELECT AuthorLast, AuthorFirst, OnHand, Title  
FROM Inventory i, Author a, Book b  
WHERE i.BookCode = b.BookCode AND 
      b.AuthorNum = a.AuthorNum; 

... and this is assuming that the columns in the SELECT part of your select statement are not ambiguous (i.e., found in more than one of the tables in the FROM part).

Michael Goldshteyn
A: 

You are using the old join syntax, you should use INNER JOIN instead.

SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM Inventory i
INNER JOIN Author a
    USING (BookCode)    -- You can also use ON, but USING remove the ambiguous columns
INNER JOIN Book b
    USING (AuthorNum);   -- Same thing here

If you get an error saying one column doesn't exist, well, you should double check it. MySQL isn't lying to you for the sake of it! You might do your query on an outdated database, for instance.

Vincent Savard
ahhh I'm a fool, I just realized there was a relationship table that had both the primary key for the author and the book. Would I use outer join in that case?
rajh2504
Outer joins are only useful if you have date in the left (or right) table and you want to join the lines even if there is nothing to join in the right (or left) table. You can join another table with INNER JOIN, it should work!
Vincent Savard