Search code examples
sqlpostgresqlwhere-clausesql-like

Multiple rows in single where ... like clause


I am trying to create a search-form and am tring to search in multiple rows/ concatenated rows. Is there a way to use something like the following code:

$where = "
    WHERE
    (
        s.companyName,
        s.companyName || s.companyType,
        s.companyName || s.companyLocationType,
        s.companyLocationName,
        s.companyLocationName || s.companyLocationType
    ) LIKE (".$val.")
";

or:

$where = "
    WHERE
    (
        s.companyName
        OR s.companyName || s.companyType
        OR s.companyName || s.companyLocationType
        OR s.companyLocationName
        OR s.companyLocationName || s.companyLocationType
    ) LIKE (".$val.")
";

Or do i have to use it like that:

$where = "
    WHERE
        s.companyName LIKE (".$val.")
        OR s.companyName || s.companyType LIKE (".$val.")
        OR s.companyName || s.companyLocationType LIKE (".$val.")
        OR s.companyLocationName LIKE (".$val.")
        OR s.companyName || s.companyLocationType LIKE (".$val.")
";

Solution

  • WHERE (".$val.") in (s.companyName | s.companyType,s.companyName | s.companyLocationType)
    

    etc. will work if you're looking for an exact match rather than using LIKE.