简体   繁体   English

MySQL 如何 select 同一张表上的多行和 WHERE 多列?

[英]MySQL How to select multiple rows with WHERE multiple columns on the same table?

I want to select room_id based on column user_id and del我想 select room_id基于列user_iddel

room_id room_id user_id用户身份 del德尔
1 1个 23 23 0 0
1 1个 45 45 0 0
1 1个 56 56 1 1个
25 25 23 23 0 0
25 25 45 45 0 0
25 25 56 56 0 0

This is an example of my table, and I want to select room_id WHERE:这是我的桌子的一个例子,我想要 select room_id WHERE:

"user_id = 23 AND del = 0" (del of the row where user_id=23) 

AND

"user_id = 45 AND del = 0" (del of the row where user_id=45) 

AND

"user_id = 56 AND del = 0" (del of the row where user_id=56) 

The query should only return room_id = 25 How do I achieve this with PHP or CodeIgniter?查询应仅返回room_id = 25如何使用 PHP 或 CodeIgniter 实现此目的?

I have tried using this and it's not working:我试过使用它但它不起作用:

$this->db->select()
->from('table')
->where(['user_id' => 23, 'del' => 0])
->where(['user_id' => 45, 'del' => 0])
->where(['user_id' => 56, 'del' => 0])
->get()->result_array();

*Notes: it worked when I tried using join tables on the same table, but when I have 10 user_id (joining the same table 10 times), the query runs super slow (because it is a big table with almost close to a million rows). *注意:当我尝试在同一个表上使用连接表时它起作用了,但是当我有 10 个user_id (连接同一个表 10 次)时,查询运行速度非常慢(因为它是一个大表,几乎有接近一百万行).

Thank you in advance.先感谢您。

Use a where clause to filter matching rows, then group the results and add a having clause:使用where子句过滤匹配的行,然后对结果进行分组并添加 having 子句:

SELECT room_id
FROM t
WHERE (user_id = 23 AND del = 0)
OR    (user_id = 45 AND del = 0)
OR    (user_id = 56 AND del = 0)
GROUP BY room_id
HAVING COUNT(*) = 3 -- there are three rows with matching condition for that room

You can try like this你可以这样试试

SELECT room_id
FROM table
WHERE user_id = 23 AND del = 0 AND user_id = 45 AND del = 0 AND user_id = 56 AND del = 0;

Then the query may look like this然后查询可能看起来像这样

$this->db->select('room_id')
->from('table')
->where(['user_id' => 23, 'del' => 0])
->orWhere(['user_id' => 45, 'del' => 0])
->orWhere(['user_id' => 56, 'del' => 0])
->get()->result_array();

You can either put all your user_id s in parentheses:您可以将所有user_id放在括号中:

SELECT room_id
FROM table
WHERE (user_id = 23 OR user_id = 45 OR user_id = 56) AND del = 0;

or you can user the WHERE IN clause:或者您可以使用WHERE IN子句:

SELECT room_id
FROM table
WHERE user_id IN (23, 45, 56) AND del = 0;

"user_id = 23 AND del = 0" will add the Room ID 1 to the result set as well. “user_id = 23 AND del = 0”也会将房间 ID 1 添加到结果集中。 Add a condition for room_id = 25 in the where clause.在 where 子句中添加 room_id = 25 的条件。

SELECT room_id
FROM
(
SELECT `room_id`,`user_id`,`del`,GROUP_CONCAT(user_id ORDER BY user_id ASC) ordered_user
FROM    `my_table`
GROUP BY room_id,del
HAVING ordered_user = "23,45,56" AND del=0
)
AS dbx

Explaination:说明:

Your Data:您的数据:

这是你的数据

Group by Room_id and Del After that filter by HAVING in ordered_user按 Room_id 和 Del 分组 之后按 ordered_user 中的 HAVING 过滤过滤后有

Make New dummy for only return 25为只返回 25 制作新的假人

可选步骤

An easier approach would be:一种更简单的方法是:

SELECT room_id
FROM table_tbl
WHERE user_id  in (23,45,56) AND del = 0 
GROUP BY room_id
HAVING COUNT(*) = 3 ;

Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/60演示: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/60

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

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