【问题标题】:Iget data from 1 table and check the data does not exist in second table从 1 个表中获取数据并检查第二个表中不存在数据
【发布时间】:2017-03-15 10:45:54
【问题描述】:

我有 3 个表创建了论坛组和 group_members 我只想获取那些不属于组成员的组以及用户 ID 当前它正在获取组,如果数据组成员表中不存在组 ID 和用户 ID仅当存在 1 个组成员时才在表中,它会拉起记录。简而言之,我想显示哪些用户未加入的组是我的三个表的表模式

Groups
+----+----------+
| id | name     |
+----+----------+
| 1  | group 1  |
| 2  | group 2  |
| 3  | group 3  |
| 4  | group 4  |
+----+----------+

forums
+------------------+-------------+
| id | title       | group_id    |
+------------------+-------------+
| 1  | test 1      |           2 |
| 2  | test 2      |           3 |
| 3  | test 3      |           2 |
| 4  | test 4      |           3 |
| 5  | test 5      |           2 |
| 6  | test 6      |           4 |
+------------------+-------------+

Group_members
+-----------------+-------------+
| id | user_id  |  group_id    |
+-----------------+-------------+
| 1  | 107       |     2        |
| 2  | 106       |     3        |
+-----------------+-------------+

这是我写的sql

<?php
$sql_grp_chk = $this->db->query("SELECT * FROM groups WHERE NOT EXISTS (SELECT * FROM group_members WHERE groups.id == group_members.group_id)");
foreach($sql_grp_chk->result() as $data_ct):
    $sql_gr_coun = $this->db->query("SELECT groups.*, (SELECT count(group_id) FROM forums WHERE groups.id = forums.group_id) as forumcount FROM groups WHERE groups.id != '".$data_ct->id."' ORDER BY forumcount DESC LIMIT 5");
    foreach($sql_gr_coun->result() as $data_count):
        $sql_follow = $this->db->get_where('group_members', array('group_id' => $data_count->id));
        var_dump($data_count);
?>

<?php endforeach; ?>
<?php endforeach; ?>

【问题讨论】:

  • 使用 NOT IN 代替 NOT EXISTS
  • 未捕获异常 'PDOException' 并带有消息 'SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“IN (SELECT * FROM group_members WHERE groups.id = group_members.group_id AND use”附近使用正确的语法
  • 您的表格中没有“用户 ID”。
  • @AlexBlex 抱歉现在检查
  • 选择查询中不应有==。​​

标签: php mysql


【解决方案1】:

不确定为什么存在forums,但要选择所有未链接到用户的组,您可以使用left join

select g.* from groups g
left join group_members m on m.group_id = g.id and m.user_id = :userId
where m.id is null;

编辑:

按链接的论坛数量选择前 5 个组:

select g.*, count(nullif(f.id, 1)) as cnt from groups g
inner join forums f on f.group_id = g.id
group by g.id
order by cnt desc
limit 5;

两个查询一起 - 前 5 个组,按链接的论坛数量,哪些用户尚未加入:

select g.*, count(nullif(f.id, 1)) as cnt from groups g
left join group_members m on m.group_id = g.id and m.user_id = :userId
left join forums f on f.group_id = g.id
where m.id is null
group by g.id
order by cnt desc
limit 5;

【讨论】:

  • 论坛之所以存在,是因为将显示哪些用户尚未加入,以及在该组上发布了更多论坛的组,您会在论坛中注意到,还有一个组 ID
  • 不会假装我理解评论,但它与“显示用户未加入的组”问题有什么关系?
  • 简单的话,有一个功能组面板,用户可以在该功能框中第二次加入,只有那些组显示在哪些组上发布了更多的论坛,所以这就是为什么我首先检查是否用户 id 存在于组成员中并且组 id 不存在然后它将检查在特定组中发布了多少个论坛和 als
  • 没关系,我添加了几个示例,说明如何在单个查询中获取数据,而不是在循环中进行多个查询。
  • 非常感谢你真的帮了我
猜你喜欢
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-13
  • 1970-01-01
  • 2016-03-12
  • 1970-01-01
相关资源
最近更新 更多