我正在尝试显示下表中的结果
id | string1 | string2
1 | mysql | php
2 | mysql | mysql
3 | python | java
我尝试过的查询
SELECT id, string1
FROM test
WHERE string1 LIKE '%mysql%' or string2 LIKE '%mysql%'
如果我从列 string1 和 string2 中搜索表中的字符串“mysql”,我希望得到的结果为
1 mysql
2 mysql
2 mysql
行 id 2 应显示多次,因为“mysql”出现在 2 列(字符串 1 和字符串 2)中。
是否有可能列出像上面这样的结果。
注意:我不想使用 UNION all
谢谢
最佳答案
您可以编写两个单独的查询,然后使用 UNION ALL 关键字将它们连接起来。类似于以下内容:
SELECT id, string1
FROM test
WHERE string1 LIKE '%mysql%'
UNION ALL
SELECT id, string2
FROM test
WHERE string2 LIKE '%mysql%';
关于php - 如果搜索字符串与 2 列匹配,则多次返回同一行 - Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20700918/