[web] [SQL] select - not in problem

Started by
3 comments, last by Kylotan 17 years, 10 months ago
ive got the following SQL command: SELECT * FROM `uvtoolslicence` WHERE orderid NOT IN (SELECT id FROM orderform) which doesnt work when i run it in php my admin. SELECT id FROM orderform -works by itself, currently returns a null set SELECT * FROM `uvtoolslicence` -also works by itself, returns 1 row, as i only have 1 row in my table. any ideas why this may be failing, this is the error message i get:

#1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT id FROM orderform )
LIMIT 0, 30' at line 1 
Advertisement
Your version of MySQL is probably too old to support sub-queries.
my web provider is runnig version:
4.0.27-standard

if this is actually the problem, is there a work around?
You are running an ancient and decrepid version of MySQL.

MySQL has had support for subqueries since 4.1, which was released quite a long time ago now.

A work-around is to use an outer join with a WHERE clause include WHERE somejoinedcolumn IS NULL.

The outer join will select all rows from the first table and join with rows from the second table where applicable, substituting NULL where there aren't any - we then select those rows with WHERE secondtable.id IS NULL (normally this ID can't be NULL, but it will be in an outer join).

As a bonus this method *may* perform better on large data sets even in databases which do support subqueries.

Mark
Something like this (adjust for syntax where necessary):

SELECT * FROM `uvtoolslicence` LEFT JOIN orderform ON uvtoolslicence.orderid = orderform.id WHERE orderform.id = NULL


EDIT: Too slow! :)

This topic is closed to new replies.

Advertisement