mysqlnested-select

Nested select statement in mysql -my code doesn't work


here is my query

SELECT con_serial,column2,column3
 FROM
 (SELECT con_serial,column2,column3
 FROM big_table 
 WHERE ISNULL(contact1, '')+'#'+ISNULL(contact2, '')+'#'+ISNULL(contact3, '')+'#'+ISNULL(contact4, '')+'#'+ISNULL(contact5, '')
 LIKE '%' + '".$conserial."' + '%') AS a
 WHERE con_serial
IN('".$contact1."','".$contact2."','".$contact3."','".$contact4."','".$contact5."')   

at the inner select i wish to get the rows which have this value $conserial in one of their 5 columns(contact1...contact5)

and the outer select to choose the rows from it that their column con_serial is one of the variables ($contact1...$contact5)

can anybody see what's wrong here?


Solution

  • Despite your new formulation, it remains very much unclear.
    Nevertheless I'll try to give you an answer, based on what I can guess...

    First here is how I'd reformulate your need:

    That said, note that you don't need to have two nested SELECT: you only want each row to satisfy two conditions, so they can be ANDed in the WHERE clause.

    Based on that, your query should be:

    $query = "
      SELECT con_serial, column2, column3
      FROM big_table
      WHERE con_serial IN ('$contact1', '$contact2', '$contact3', '$contact4', '$contact5')
      AND '$con_serial' IN (contact1, contact2, contact3, contact4, contact5)
    ";