SELECT * FROM表WHERE列LIKE CONCAT('%',<other sql =“”query =“”limit =“”1 =“”>,'%') - 如何?</other>

时间:2010-10-23 17:37:19

标签: mysql sql sql-like

如何将这两个查询合并为一个?

1)这找到狗的日本标志(犬):

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

2)这会找到所有带有“狗”(犬)标志的日语单词:

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

3)我将这两者合二为一,因为这不起作用?!

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

2 个答案:

答案 0 :(得分:20)

括号内容很重要,因此,请尝试以下方法:

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

但是,告诉我们你收到的错误可能会很好。

答案 1 :(得分:5)

使用:

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

我不建议使用LIMIT,但如果您真的需要它,请使用:

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