我正在使用三個表:
- users 表包含用戶 ID、名稱和狀態
- supply_history 表包含 id、users_id、amount 和 status
- denied_deal 表包含 id、users_id、rejected_amount 和狀態。
我試圖找到的是總結每個用戶的supply_amount 和rejected amount,其中用戶狀態、supply_history 狀態和rejected_deal 狀態為1。
這是我到目前為止所嘗試的。
SELECT users.id, users.name,
sum(supply_history.amount) as supply_amount,
sum(rejected_deal.rejected_amount) as rejected_amount
from users
inner join supply_history on supply_history.users_id=users.id
inner join rejected_deal on rejected_deal.users_id=users.id
where users.status='1' and rejected_deal.status='1' and supply_history.status='1'
GROUP by users.id;
我當前的查詢結果如下。
| ID | 姓名 | 供應量 | 拒絕金額 |
|---|---|---|---|
| 1 | 船長 | 50 | 20 |
| 2 | 私人的 | 200 | 20 |
我正在尋找的結果
| ID | 姓名 | 供應量 | 拒絕金額 |
|---|---|---|---|
| 1 | 船長 | 50 | 10 |
| 2 | 私人的 | 100 | 20 |
貝婁是我的三張桌子
- 用戶表。
| ID | 姓名 | 狀態 |
|---|---|---|
| 1 | 船長 | 1 |
| 2 | 私人的 | 1 |
- 供應歷史表。
| ID | users_id | 數量 | 狀態 |
|---|---|---|---|
| 1 | 1 | 20 | 1 |
| 2 | 1 | 30 | 1 |
| 3 | 2 | 100 | 1 |
- 拒絕交易表。
| ID | users_id | 拒絕金額 | 狀態 |
|---|---|---|---|
| 1 | 2 | 15 | 1 |
| 2 | 2 | 5 | 1 |
| 3 | 1 | 10 | 1 |
我該如何解決這個查詢請幫忙。謝謝你。
uj5u.com熱心網友回復:
您將supply_history 行和rejected_deal 行加入到用戶行。但是supply_history 和rejected_deal 沒有直接關系。因此,對于一個用戶,您創建了 supply_history 和 denied_deal 的所有組合。結果是:
| ID | 姓名 | 狀態 | sh_id | users_id | 數量 | 狀態 | rd_id | users_id | 拒絕金額 | 狀態 |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 船長 | 1 | 1 | 1 | 20 | 1 | 2 | 2 | 5 | 1 |
| 1 | 船長 | 1 | 2 | 1 | 30 | 1 | 3 | 1 | 10 | 1 |
| 2 | 私人的 | 1 | 3 | 2 | 100 | 1 | 1 | 2 | 15 | 1 |
| 2 | 私人的 | 1 | 3 | 2 | 100 | 1 | 1 | 2 | 15 | 1 |
當您聚合此資料集時,您會將金額加起來數倍。(每個值都乘以另一個表中用戶的行數)。
而是將總和加入用戶:
select u.id, u.name, sh.supply_amount, rd.rejected_amount
from users u
left outer join
(
select users_id, sum(amount) as supply_amount
from supply_history
where status = 1
group by users_id
) sh on sh.users_id = u.id
left outer join
(
select users_id, sum(rejected_amount) as rejected_amount
from rejected_deal
where status = 1
group by users_id
) rd on rd.users_id = u.id
where u.status = 1
order by u.id;
對于用戶在兩個表中都沒有條目的情況,我已將您的內部聯接轉換為外部聯接。如果您只希望用戶在兩個表中都有資料,則將其更改回內部聯接。
如果你想提高查詢的可讀性,你可以考慮使用WITH子句:https ://dev.mysql.com/doc/refman/8.0/en/with.html 。
uj5u.com熱心網友回復:
像這樣用于Over總和
SELECT users.id, users.name,
sum(supply_history.amount) OVER(PARTITION BY users.id, ORDER BY users.id) as supply_amount,
sum(rejected_deal.rejected_amount) OVER(PARTITION BY users.id, ORDER BY users.id) as rejected_amount
from users
inner join supply_history on supply_history.users_id=users.id
inner join rejected_deal on rejected_deal.users_id=users.id
where users.status='1' and rejected_deal.status='1' and supply_history.status='1'
uj5u.com熱心網友回復:
嘗試使用子查詢是這樣的:
SELECT users.id, users.name,
(select sum(amount) from supply_history s where s.users_id = users.id and s.status = '1' ) as supply_amount ,
(select sum(rejected_amount) from rejected_deal r where r.users_id = users.id and r.status = '1' ) as rejected_amount
from users
where users.status='1';
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/456406.html
上一篇:如何解決查詢中的模棱兩可的錯誤
