0
我有三個表設置用於跟蹤參加事件。這些表如下:MySQL:基於事件類型計數事件出勤
Users
--------
rid
name
Events
--------
eid
name
type (either 0 or 1)
Attendance
--------
rid
eid
我試圖返回的所有用戶,並打印出他們參加過破入量1型和0
事件的數量的計數我可以成功地返回一個或另一個但不是兩者的用下面的語句組合:
SELECT users.name, COUNT(type) as typeCount FROM attendance, events, users
WHERE users.rid=attendance.rid && events.eid=attendance.eid && type=1 GROUP BY users.rid
它返回
╔══════╦══════════════╗
║ name ║ typeCount ║
╠══════╬══════════════╣
║ John ║ 5 ║
╠══════╬══════════════╣
║ Jack ║ 3 ║
╚══════╩══════════════╝
但我想它返回
╔══════╦══════════════╦═════════════╗
║ name ║ type0Count ║ type1Count ║
╠══════╬══════════════╬═════════════╣
║ John ║ 2 ║ 5 ║
╠══════╬══════════════╬═════════════╣
║ Jack ║ 7 ║ 3 ║
╚══════╩══════════════╩═════════════╝
在case語句中添加'END'。 – 7alhashmi