【问题标题】:MySQL How to select multiple rows with WHERE multiple columns on the same table?MySQL 如何在同一张表上选择多行 WHERE 多列?
【发布时间】:2021-11-17 14:55:02
【问题描述】:

我想根据user_iddel 列选择room_id

room_id user_id del
1 23 0
1 45 0
1 56 1
25 23 0
25 45 0
25 56 0

这是我的桌子的一个例子,我想选择 room_id WHERE:

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

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

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

查询应该只返回room_id = 25 如何使用 PHP 或 CodeIgniter 实现这一点?

我尝试过使用它,但它不起作用:

$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();

*注意:当我尝试在同一个表上使用连接表时它起作用了,但是当我有 10 个user_id(连接同一个表 10 次)时,查询运行速度非常慢(因为它是一个大表,几乎接近一百万行)。

提前谢谢你。

【问题讨论】:

    标签: php mysql codeigniter codeigniter-3


    【解决方案1】:

    更简单的方法是:

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

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

    【讨论】:

      【解决方案2】:
      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
      

      说明:

      您的数据:

      按 Room_id 和 Del 分组 在ordered_user中的HAVING过滤之后

      为仅返回 25 制作新的虚拟对象

      【讨论】:

        【解决方案3】:

        使用where 子句过滤匹配的行,然后对结果进行分组并添加have 子句:

        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
        

        【讨论】:

          【解决方案4】:

          "user_id = 23 AND del = 0" 也会将房间 ID 1 添加到结果集中。 在 where 子句中添加 room_id = 25 的条件。

          【讨论】:

            【解决方案5】:

            您可以将所有user_ids 放在括号中:

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

            或者您可以使用WHERE IN 子句:

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

            【讨论】:

              【解决方案6】:

              你可以这样试试

              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;
              

              那么查询可能如下所示

              $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();
              

              【讨论】:

              • 您好,谢谢,但是这个不行,已经试过了
              猜你喜欢
              • 1970-01-01
              • 2011-05-02
              • 1970-01-01
              • 2013-02-28
              • 1970-01-01
              • 2012-04-04
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多