tags:

views:

62

answers:

1

I am trying to run a query on a SQLite database that INNER JOINs two additional tables:

SELECT
    usages.date AS date,
    usages.data AS data,
    stores.number AS store,
    items.name AS item
FROM usages
INNER JOIN stores USING (store_id)
INNER JOIN items USING (item_id)

However, I get the error

SQL error: cannot join using column item_id - column not present in both tables

I know I can use the explicit INNER JOIN stores ON usages.store_id = stores.store_id (and it works), but: why does the USING query throw an error in SQLite?

It doesn't on MySQL...

I should note:
This isn't a problem for me, as I am using the ON syntax, but I would like to know why this is happening.

+1  A: 

So you have:

INNER JOIN items USING (item_id)

...and you get an error that says:

SQL error: cannot join using column item_id - column not present in both tables

That's got to be one of the least cryptic error messages I've seen.
What I don't like is that it's not clear to me what table is being compared to ITEMS.item_id - is it STORES or USAGES? Which is why I refrain from the USING or NATURAL join syntax...

OMG Ponies
Yes, I thought about that, however, I tried adding parentheses around it to clarify: `FROM ((usages INNER JOIN stores USING (store_id)) INNER JOIN items USING (item_id))`, however, that did not work.
Austin Hyde
@Austin Hyde: Brackets only work within the ON conditions - they don't give precedence to JOINs. I'd like to know where the school of thought comes from that throwing brackets at anything in SQL will save the day... Martin and I tread ilghtly - it's time to be more direct: Abandon the `USING` syntax and fix your query.
OMG Ponies
@OMG Ponies: Well, in regards to your "school of thought" comment, I never really *learned* SQL - I've just slowly been assimilating its syntax when I need to do something I haven't done before. In this case, I read about the `ON` syntax and how it was most supported, then about the less-supported-but-still-standard `USING` syntax, and thought I'd give it a go because it is simpler. As soon as it didn't work, I switched to the `ON` syntax with no problems. I was just wondering why `JOIN`ing multiple tables wasn't working with `USING`.
Austin Hyde