MYSQL:返回与多个搜索条件匹配的内容

时间:2016-10-26 11:42:58

标签: mysql

这是我的表结构。

我有一个像

这样的查询
SELECT `user_pid` from `table` WHERE 
`upchain` LIKE '%,33,%' OR `upchain` LIKE '%,52,%' OR upchain LIKE '%,98,%'

给出的结果如下:

enter image description here

但我需要知道哪一行符合哪个搜索条件。 有没有办法得到以下结果:

user_pid | search_critetis
--------------------------
  33     |    ,33,
  52     |    ,52,
  98     |    ,98,
  100    |    ,98,
  101    |    ,98,

3 个答案:

答案 0 :(得分:0)

如果可能,您可以将查询重写为

SELECT `user_pid` ,'%,33,%' from `table` WHERE 
`upchain` LIKE '%,33,%' 
 union all
SELECT `user_pid` ,'%,52,%' from `table` WHERE 
 `upchain` LIKE '%,52,%' 

您还可以使用case

重写查询

答案 1 :(得分:0)

尝试使用SELECT user_pid, CASE WHEN upchain LIKE '%,33,%' THEN ',33,' ELSE CASE WHEN upchain LIKE '%,52,%' THEN ',52,' ELSE CASE WHEN upchain LIKE '%,98,%' THEN ',98,' END END END as upchain FROM table WHERE upchain LIKE '%,33,%' OR upchain LIKE '%,52,%' OR upchain LIKE '%,98,%'

{{1}}

答案 2 :(得分:0)

SELECT 
user_pid,
IF( upchain LIKE '%,33,%', '%,33,% condition' , 
    IF(
        upchain like '%,52,%', '%,52,% condition', 
        IF(
            upchain like '%,98,%', '%,98,% condition', 
            'other condition'
        )               
    ) 
) 
as condition from table

您可以使用此语法并根据需要添加多个条件。

IF('your condition', << true part >>, "another condition OR elase part")
相关问题