Using 'LIKE' with the result of a SQL subquery

15.2k Views Asked by At

The following query is working absolutely fine for me:

SELECT * From Customers
WHERE Customers.ContactName = (SELECT FirstName
                               FROM Employees as E, orders as O
                               WHERE <condition>
                               LIMIT 1);

However, if i use LIKE instead of = to compare with the result of the subquery, I'm not getting any results. How do I use LIKE '%%' in the above query?

8

There are 8 best solutions below

1
On BEST ANSWER

First, this query should not be working fine:

SELECT *
From Customers
WHERE Customers.ContactName = (SELECT FirstName
                               from Employees as E, orders as O
                               WHERE LIMIT 1);

Because WHERE LIMIT 1 is not proper SQL. And, you should learn to use proper join syntax. Presumably, you intend:

SELECT c.*
From Customers c
WHERE c.ContactName = (SELECT FirstName
                       FROM Employees as E JOIN
                            Orders as O
                            ON . . .
                       LIMIT 1
                      );

You could conceivably add LIKE instead of = and '%' in the subquery:

WHERE c.ContactName LIKE (SELECT CONCAT('%', FirstName, '%') . . .

But I would write this using EXISTS:

SELECT c.*
From Customers c
WHERE EXISTS (SELECT 1
              FROM Employees as E JOIN
                   Orders as O
                   ON . . .
              WHERE c.ContactName LIKE CONCAT('%', FirstName, '%')
             );

This does not do exactly the same thing as your query. It does something more reasonable. Instead of comparing one random name from the subquery, it will determine if there are any matches in the subquery. That seems a more reasonable intention for the query.

1
On

I guess this should work for you, no?

SELECT * From Customers
WHERE Customers.ContactName LIKE '%' + (
        SELECT FirstName from Employees as E, orders as O
        WHERE <condition>
        LIMIT 1
    ) + '%';
0
On

SQL Server

This will return specific keyword

select * 
from Customers
where Customers.ContactName like (select top 1 FirstName from Employees )

This will return if keyword is exist

select * 
from Customers
where Customers.ContactName like '%' + (select top 1 FirstName from Employees) + '%'
0
On

I would suggest:

SELECT * From Customers as a
INNER JOIN (SELECT '%'+FirstName+'%' as FirstName from Employees as E, orders as O
                               WHERE <condition>
                               LIMIT 1) as b
ON a.ContactName LIKE b.Firstname;
0
On

If you have foreign key to employee table then you do not need to use LIKE. However if you want to use it you can try following

select * 
from customers a, employees b 
where (contions on employees) 
  and customers.contactname like '%'||b.firstname||'%';
0
On

Using your approach, you would need to wrap the FirstName field with '%' either side to put wildcards in your selected values.

So, something like

SELECT * From Customers 
 WHERE Customers.ContactName LIKE (SELECT '%' + FirstName + '%'
                                    from Employees as E
                                   inner join, orders as O on...
                                   WHERE <condition>
                                   LIMIT 1);

Seems to me like you would be better off joining your tables. Maybe this would be better

SELECT c.* 
  FROM Customers c
 INNER JOIN Employees e on c.ContactName like '%' + e.FirstName + '%'
 WHERE <condition>
0
On

The following query is MSSQL statement, Instead of LIKE, I used CHARINSDEX. Try the relevant function in MySQL (check INSTR function) instead of CHARINDEX.

SELECT
    *
FROM
    Customers
WHERE
    CHARINDEX(( SELECT  TOP 1 FirstName
                FROM    Employees as E, orders as O
                WHERE   <condition>
                ), Customers.ContactName, 1
             ) > 0;

I am not sure, but you can try the following in MySQL.

SELECT
    *
FROM
    Customers
WHERE
    INSTR(Customers.ContactName,
          ( SELECT  FirstName
            FROM    Employees as E, orders as O
            WHERE   <condition>
            LIMIT 1)) > 0;
0
On

Why not use a simple INNER JOIN:

SELECT Customers.*
FROM Customers
INNER JOIN Employees ON Customers.ContactName LIKE CONCAT('%', Employees.FirstName,'%')
WHERE Employees.Foo = 'Bar'

Note: + is the addition operator and can not be used to concatenate strings.