MySQL 在这里加入最佳选择?

发布于 2024-11-08 02:52:06 字数 1168 浏览 0 评论 0原文

活动:

id     name            cap
1      Campaign1       2
2      Campaign2       1
3      Campaign3       1

服务:

id     id_campaign     ip
1      1               127.0.0.1
2      1               127.0.0.1
3      2               127.0.0.1

查询的结果应显示:

campaigns_id    campaigns_name          cap    count
1               Campaign1               2      2
2               Campaign2               1      1
3               Campaign3               1      0

我正在使用此查询:

   SELECT served.id_campaign, 
          campaigns.name, 
          campaigns.cap, 
          COUNT( served.id ) AS count
     FROM campaigns
LEFT JOIN served ON campaigns.id = served.id_campaign
    WHERE served.ip =  '127.0.0.1' 
 GROUP BY served.id_campaign

我的查询正在显示这些结果(不好,未显示campaigns_id.3):

campaigns_id    campaigns_name          cap    count
1               Campaign1               2      2
2               Campaign2               1      1

我认为如果我想包含不包含的值,我正在接近错误t 被“WHERE”语句选择,因为“served”中没有与 Campaigns.id='3' 的 WHERE 语句匹配的记录

campaigns:

id     name            cap
1      Campaign1       2
2      Campaign2       1
3      Campaign3       1

served:

id     id_campaign     ip
1      1               127.0.0.1
2      1               127.0.0.1
3      2               127.0.0.1

The result of the query should display:

campaigns_id    campaigns_name          cap    count
1               Campaign1               2      2
2               Campaign2               1      1
3               Campaign3               1      0

I am using this query:

   SELECT served.id_campaign, 
          campaigns.name, 
          campaigns.cap, 
          COUNT( served.id ) AS count
     FROM campaigns
LEFT JOIN served ON campaigns.id = served.id_campaign
    WHERE served.ip =  '127.0.0.1' 
 GROUP BY served.id_campaign

My query is displaying these results (bad, not showing campaigns_id.3):

campaigns_id    campaigns_name          cap    count
1               Campaign1               2      2
2               Campaign2               1      1

I am thinking I am approaching wrong if I want to include values that aren't being selected by the "WHERE" statement as there are no records in "served" that match the WHERE statement for campaigns.id='3'

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

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

发布评论

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

评论(3

瑕疵 2024-11-15 02:52:06

新编辑:

   SELECT campaigns.id as id_campaign, 
          campaigns.name, 
          campaigns.cap, 
          COUNT( served.id ) AS count
     FROM campaigns
LEFT JOIN served ON campaigns.id = served.id_campaign
      AND served.ip =  '127.0.0.1' 
 GROUP BY campaigns.id

旧帖子:

我想出了这个查询,它会产生您想要的结果:

(SELECT served.id_campaign, 
          campaigns.name, 
          campaigns.cap, 
          COUNT( served.id ) AS count
     FROM campaigns
LEFT JOIN served ON campaigns.id = served.id_campaign
    WHERE served.ip =  '127.0.0.1' 
 GROUP BY served.id_campaign)

UNION

(SELECT id, name, cap, 0 as count 
FROM campaigns 
WHERE id <> ALL 
     (
      SELECT campaigns.id 
      FROM campaigns, served 
      WHERE campaigns.id = served.id_campaign AND served.ip = "127.0.0.1"
     )
)

NEW EDIT:

   SELECT campaigns.id as id_campaign, 
          campaigns.name, 
          campaigns.cap, 
          COUNT( served.id ) AS count
     FROM campaigns
LEFT JOIN served ON campaigns.id = served.id_campaign
      AND served.ip =  '127.0.0.1' 
 GROUP BY campaigns.id

OLD POST:

I came up with this query, which produces the result you want:

(SELECT served.id_campaign, 
          campaigns.name, 
          campaigns.cap, 
          COUNT( served.id ) AS count
     FROM campaigns
LEFT JOIN served ON campaigns.id = served.id_campaign
    WHERE served.ip =  '127.0.0.1' 
 GROUP BY served.id_campaign)

UNION

(SELECT id, name, cap, 0 as count 
FROM campaigns 
WHERE id <> ALL 
     (
      SELECT campaigns.id 
      FROM campaigns, served 
      WHERE campaigns.id = served.id_campaign AND served.ip = "127.0.0.1"
     )
)
风筝有风,海豚有海 2024-11-15 02:52:06

将 where 子句更改为

WHERE served.ip =  '127.0.0.1' OR served.ip IS NULL

Change your where clause to

WHERE served.ip =  '127.0.0.1' OR served.ip IS NULL
时光病人 2024-11-15 02:52:06

LEFT JOIN 表示从左表中取出所有记录,并从右表中取出匹配的记录。因此,served.ip 将从 id=3 开始为 NULL。
所以改变where条件就可以了

WHERE serverd.ip = '127.0.0.1' or serverd.ip IS NULL

LEFT JOIN says that bring all the records from left table and the matching records from the right hand side table. So the served.ip would be NULL from id=3.
So change the where condition to make it

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