Search code examples
phpmysqlsqlmysql-error-1242

SQL SELECT Question


I am trying to perform a SQL query that acts in two parts.

First, I have a query that returns a list of 10 Ids. But then I want to have a SELECT statement which has a WHERE clause for each of these 10 ids.

Is this possible?

I tried:

    SELECT * FROM tablenameWHERE id= (SELECT id FROM table_of_ids WHERE 
    tableid='1a177de1-3f25c9b7910b' OR 
    tableid='64faecca-133af807a65a' OR
... up to 10 Ids)

but it returns with an error stating the subquery returns more than 1 row.

Note, the tableid and id columns of table_of_ids are different values.

Does anyone know how to accomplish this? I seem to be at a loss myself.

If it matters, I am using mySQL and PHP.

Cheers, Brett


Solution

  • Not a very optimized query, but you could use IN instead of =

     SELECT * FROM tablename WHERE id IN (SELECT id FROM table_of_ids WHERE 
        tableid='1a177de1-3f25c9b7910b' OR 
        tableid='64faecca-133af807a65a' OR
    ... up to 10 Ids)