Php 在左侧联接查询中显示0个结果
如何计算event_Attendants(事件参与者)表中的参与者人数,并显示结果为零的事件标题。这是我的桌子Php 在左侧联接查询中显示0个结果,php,mysqli,Php,Mysqli,如何计算event_Attendants(事件参与者)表中的参与者人数,并显示结果为零的事件标题。这是我的桌子 event_title id | name 1 | New York 2 | Canada event_participants id | event_title_id | name 1 | 1 | Jon 2 | 1 | Mike 这是我的问题 SELECT count( * ) AS count FRO
event_title
id | name
1 | New York
2 | Canada
event_participants
id | event_title_id | name
1 | 1 | Jon
2 | 1 | Mike
这是我的问题
SELECT count( * ) AS count
FROM event_participants AS t1
LEFT JOIN event_title AS t2 ON t1.event_title_id = t2.id
GROUP BY t1.event_title_id
我得到了正确的结果,但没有显示0结果。
我应该得到这样的结果
New York | 2
Canada | 0
问题出在你的组员身上。 您正在按
事件\u title\u id
分组,其中您没有id2
您应该按照第一个表的名称
进行分组
SELECT count( * ) AS count
FROM event_participants AS t1
LEFT JOIN event_title AS t2 ON t1.event_title_id = t2.id
GROUP BY t2.name
应该起作用代替
*
使用t2.id
它会起作用
SELECT count(t2.id) AS count
FROM event_participants AS t1
LEFT JOIN event_title AS t2 ON t1.event_title_id = t2.id
GROUP BY t1.event_title_id
按以下方式更改查询以获得所需的输出:
SELECT count(t2.id) AS count
FROM event_title AS t1
LEFT JOIN event_participants AS t2 ON t2.event_title_id = t1.id
GROUP BY t1.id
//To retrieve title also
SELECT t1.title,count(t2.id) AS count
FROM event_title AS t1
LEFT JOIN event_participants AS t2 ON t2.event_title_id = t1.id
GROUP BY t1.id
你如何确定你得到了正确的结果???你在用PHP吗???如果是,请向我们展示您的PHP代码。。!