Using 'LIKE' with the result of a SQL subquery

牧云@^-^@ 提交于 2021-02-07 14:37:39

问题


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?


回答1:


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.




回答2:


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;



回答3:


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
    ) + '%';



回答4:


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.




回答5:


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>



回答6:


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;



回答7:


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||'%';



回答8:


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) + '%'


来源:https://stackoverflow.com/questions/30889745/using-like-with-the-result-of-a-sql-subquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!