简体   繁体   English

如果使用mysql匹配运算符值,如何从表中获取所有行?

[英]How to fetch all rows from table if like operator value match using mysql?

I'm trying to fetch all record if like operator value match and is true. 我想,如果运营商价值的比赛,是真正获取所有记录。

I have table ticket. 我有桌票。

---------------------------
: id  :  ticket : user_id :
---------------------------
: 1   : 2546    : 2       :
---------------------------
: 2    : 8475    : 2      :
---------------------------

I'm trying this query 我正在尝试这个查询

SELECT * FROM `ticket` WHERE `ticket` LIKE '%2546%'  

Above query is returning single result and is working fine. 以上查询返回单个结果并且工作正常。 But I need both rows if like operator value match and table has more record of that row user_id . 但我需要两行,如果运营商价值的匹配和表有该行的user_id更多的记录。 I tried group by 我试过分组

SELECT * FROM `ticket` WHERE `ticket` LIKE '%2546%' 
GROUP BY `user_id `

I know it can be done if use user_id = 2 instead of like and Group By operator but I need to filter by ticket column. 我知道如果使用user_id = 2而不是likeGroup By运算符可以完成,但我需要按ticket列过滤。 So is this possible to achieve this kind of task in single query? 那么这可以在单个查询中实现这种任务吗?

Use a nested query: 使用嵌套查询:

SELECT *
FROM ticket t1
WHERE user_id IN (
    SELECT DISTINCT user_id
    FROM ticket t2
    WHERE ticket LIKE '%2456%'
);

Using self join as 使用自联接作为

SELECT U . * 
FROM  `ticket` t, `ticket` U
WHERE t.`ticket` LIKE  '%2546%'
AND t.`user_id` = U.`user_id` 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM