我試圖通過 join 連接兩個表并將它們分組以獲得計數。但不幸的是,這兩個表沒有任何共同的價值可以加入(或者我誤解了解決方案)。
select date_format(check_in.date,'%M') as Month, count(check_in.Id) as checkInCount
from check_in
group by month(check_in.date);
| 月 | 簽入計數 |
|---|---|
| 七月 | 1 |
| 十月 | 2 |
這是第一張桌子。
select date_format(reservation.date,'%M') as Month, count(reservation.id) as reserveCount
from reservation
group by month(reservation.date);
| 月 | 保留計數 |
|---|---|
| 七月 | 3 |
| 十月 | 5 |
這是第二張桌子。我想在一張表中顯示這兩個表。
| 月 | 簽入計數 | 保留計數 |
|---|---|---|
| 七月 | 1 | 3 |
| 十月 | 2 | 5 |
感謝您嘗試此操作,如果這太簡單了,我們深表歉意。
uj5u.com熱心網友回復:
您需要month從兩個子查詢中加入結果。此查詢假設所有month(七月,八月,九月...)出現在你的子查詢monthCheckInStat,monthCheckOutStat即使計數為0
SELECT monthCheckInStat.Month, monthCheckInStat.checkInCount, monthCheckOutStat.reserveCount
FROM
(
select date_format(check_in.date,'%M') as Month, count(check_in.Id) as checkInCount
from check_in
group by month(check_in.date)
) monthCheckInStat
INNER JOIN
(
select date_format(reservation.date,'%M') as Month, count(reservation.id) as reserveCount
from reservation
group by month(reservation.date)
) monthCheckOutStat
ON monthCheckInStat.Month = monthCheckOutStat.Month;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/315208.html
