从多个LIKE子句中查找哪个列与LIKE匹配?

时间:2014-08-24 20:16:06

标签: mysql sql-like

SELECT COUNT(news_id) AS count 
FROM fusion_news 
WHERE (news_subject LIKE '%новым%' || news_news LIKE '%новым%')

有没有办法可以使用别名或设置一些标志来查看哪个列匹配?目前我只是在完成news_newsnews_subject匹配的查询之后,在PHP中进行手动检查,但是如果可能的话,我可以通过单独的SQL以某种方式标记这一点吗?

2 个答案:

答案 0 :(得分:2)

你可以使用UNION ..考虑:

Select count(1) as Count, "in News Subject" as Source
  from fusion_news
 where news_subject LIKE '%новым%'
UNION
Select count(1) as Count, "in News Body" as Source
  from fusion_news
 where news_news LIKE '%новым%'

或者,如果你想要计算细分     选择 (             选择count(1)作为Count               来自fusion_news               其中news_subject LIKE'%новым%')作为SubjectCount,     选择 (             选择count(1)作为Count               来自fusion_news               news_news LIKE'%новым%')as Content Count

答案 1 :(得分:1)

您可以将其作为布尔值查询。当被视为数字时,Mysql会将true转换为1并将false转换为0,因此您只需sum where中的布尔表达式子句:

SELECT SUM(news_subject LIKE '%новым%') AS news_subject_num_matches, 
       SUM(news_news LIKE '%новым%') AS news_news_num_matches
FROM   fusion_news 
WHERE  (news_subject LIKE '%новым%' || news_news LIKE '%новым%')