Jump to content

MYSQL, SELECT id WHERE id DOES NOT EQUAL MULTIPLE VALUES


sloth456

Recommended Posts

Hi guys,

 

I need to select 1 random row from a table but the row id must NOT match one of multiple values (possibly thousands).

 

To put it another way:

 

SELECT any random row EXCEPT ones WHERE id="1,6,9,100,300,56,2,44......"

 

Whats a good way to do this considering there could be more than 3,000 id's we would not want to pick from.

 

Any help would be greatly appreciated.

Hi

 

You could also use a JOIN

 

SELECT DISTINCT table1.id
FROM table1
LEFT OUTER JOIN table2.id
WHERE table2.id IS NULL

 

Used to be far more efficient to do it this way, but think with newer versions of MySQL the 2 methods are far more evenly matched.

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.