SELECT * FROM tbl WHERE clm LIKE CONCAT('%',<other sql query LIMIT 1>,'%') - HOW?

梦想的初衷 提交于 2019-11-30 03:25:20

问题


How can I combine those two queries into one?

1) This finds the japanese sign for dog (犬):

SELECT japanese 
  FROM edict 
 WHERE english LIKE 'dog' 
 LIMIT 1;

2) This finds all japanese words with the sign for 'dog' (犬) in it:

SELECT japanese 
  FROM edict 
 WHERE japanese LIKE '%犬%';

3) I am having trouble combining those two into one, because this doesn't work?!

SELECT japanese 
FROM edict 
WHERE japanese
LIKE CONCAT('%',
    SELECT japanese FROM edict WHERE english LIKE 'dog' LIMIT 1,'%'
);

回答1:


Parenthesises are important, therefore, try this :

SELECT japanese
FROM edict
WHERE japanese LIKE CONCAT('%', 
                           (SELECT japanese FROM edict WHERE english LIKE 'dog' LIMIT 1), 
                           '%');

It might have been good to tell us what error you received, though.




回答2:


Use:

SELECT a.japanese 
  FROM EDICT a
  JOIN EDICT b ON b.japanese = a.japanese
 WHERE b.english LIKE 'dog'

I don't recommend the use of LIMIT, but if you really need it for this, use:

SELECT a.japanese 
  FROM EDICT a
  JOIN (SELECT t.japanese
          FROM EDICT t
         WHERE t.english LIKE 'dog'
         LIMIT 1) b ON b.japanese = a.japanese


来源:https://stackoverflow.com/questions/4005251/select-from-tbl-where-clm-like-concat-other-sql-query-limit-1-how

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