2左连接的语法在sqlite中加入

发布于 2025-01-22 07:20:53 字数 942 浏览 0 评论 0原文

此查询运行良好,并给我一个这样的输出:

[(1, 9), (2, 12), (4, 14), (6, 14)]
    query = """
    SELECT users.id,
       count(tasks.userId)
    FROM users
    LEFT JOIN tasks ON users.id = tasks.userId
    WHERE tasks.completed = FALSE
    GROUP BY users.id
    """

但是,当我添加另一个左JOIN时,它并不能给我准确的结果:

    query = """
    SELECT users.id,
       count(tasks.userId), count(songs.userId)
    FROM users
    LEFT JOIN tasks ON users.id = tasks.userId
    LEFT JOIN songs ON users.id = songs.userId
    WHERE tasks.completed = FALSE
    GROUP BY users.id
    """

结果看起来像这样:

[(1, 9, 10), (2, 12, 10), (4, 14, 10), (6, 14, 10)]

但是,我的结果看起来像这样:

[(1, 90, 90), (2, 120, 120), (4, 140, 140), (6, 140, 140)]

看起来像值任务x 10 as count(任务)和计数(歌曲) 我错过了什么?

count(tasks.userid)的想法是查找用户ID匹配的任务数。

This query runs fine and gives me an output like this:

[(1, 9), (2, 12), (4, 14), (6, 14)]
    query = """
    SELECT users.id,
       count(tasks.userId)
    FROM users
    LEFT JOIN tasks ON users.id = tasks.userId
    WHERE tasks.completed = FALSE
    GROUP BY users.id
    """

However, when I add another left join, it does not give me accurate results:

    query = """
    SELECT users.id,
       count(tasks.userId), count(songs.userId)
    FROM users
    LEFT JOIN tasks ON users.id = tasks.userId
    LEFT JOIN songs ON users.id = songs.userId
    WHERE tasks.completed = FALSE
    GROUP BY users.id
    """

The result should look like this:

[(1, 9, 10), (2, 12, 10), (4, 14, 10), (6, 14, 10)]

but instead, my result looks like this:

[(1, 90, 90), (2, 120, 120), (4, 140, 140), (6, 140, 140)]

which looks like the value of tasks x 10as both, count(tasks) and count(song)
what am I missing out on?

The idea of count(tasks.userId)was to find the number of tasks where the userId matched.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见终念 2025-01-29 07:20:53

任务歌曲中分别汇总,然后将用户加入聚合的结果:

SELECT u.id, 
       COALESCE(t.count_tasks, 0) count_tasks,
       COALESCE(s.count_songs, 0) count_songs
FROM users u
LEFT JOIN (
  SELECT userId, COUNT(*) count_tasks
  FROM tasks 
  WHERE completed = FALSE
  GROUP BY userId
) t ON u.id = t.userId
LEFT JOIN (
  SELECT userId, COUNT(*) count_songs
  FROM songs  
  GROUP BY userId
) s ON u.id = s.userId;

我不确定您是否真的想要>左加入(至少对于表任务),因为在您的代码中,您所说的第一个查询返回了您的期望,尽管它包含left加入,加入实际上是内部 JOIN,因为条件wend tasks.completed = false仅返回匹配行。

Aggregate separately in tasks and songs and then join users to the results of the aggregations:

SELECT u.id, 
       COALESCE(t.count_tasks, 0) count_tasks,
       COALESCE(s.count_songs, 0) count_songs
FROM users u
LEFT JOIN (
  SELECT userId, COUNT(*) count_tasks
  FROM tasks 
  WHERE completed = FALSE
  GROUP BY userId
) t ON u.id = t.userId
LEFT JOIN (
  SELECT userId, COUNT(*) count_songs
  FROM songs  
  GROUP BY userId
) s ON u.id = s.userId;

I'm not sure if you actually want LEFT joins (at least for the table tasks), because in your code, the 1st query that you say returns what you expect, although it contains a LEFT join, the join is actually an INNER join because the condition WHERE tasks.completed = FALSE returns only the matching rows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文