은 상정 :SQL 중첩 요청
transaction
- 사용자 밸런스 변화에 대한 일반 정보.primary key : transaction_id
.bet_transactions
- 내기에 대한 특별한 정보,foreign key : transaction_id
.win_transaction
-bet_transactions
와 동일하지만,wins
을 포함합니다.
나는 모든 베팅의 합계를 얻기 위해 원하는 승리. 나는이 함께했다 :
이select
u.username as username,
u.balance as balance,
sum(bt.amount) as bet_sum,
sum(wt.amount) as win_sum
from
users u,
(
select
t.*
from
transactions t,
bet_transactions b
where
t.transaction_id = b.transaction_id
) bt,
(
select
t.*
from
transactions t,
win_transactions w
where
t.transaction_id = w.transaction_id
) wt
where
u.username = bt.username and
u.username = wt.username
group by
u.username
이 제대로 작동하지 않습니다 : 난 항상 하나의 행을 얻을 합계가 올바르지 않습니다. 그러나이 중첩 된 부분 중 하나를 제거하면 예상대로 작동하기 시작합니다. 내가 도대체 뭘 잘못하고있는 겁니까?
select
u.username as username,
u.balance as balance,
sum(bt.amount) as bet_sum
from
users u,
(
select
t.*
from
transactions t,
bet_transactions b
where
t.transaction_id = b.transaction_id
) bt
where
u.username = bt.username
group by
u.username
이것은 1980 년대에 사용 된 조인 구문입니다. 그것은 1992 년에 불필요하게 만들어졌습니다. 더 이상 사용하면 안됩니다. 대신에 적절한 ANSI 조인을 사용하십시오.이 조인은 오류가 적고 읽기 및 유지 관리가 쉽습니다 ('트랜잭션에서 t_transaction_id = b.transaction_id'에 bet_transactions b에 참여하십시오). –
금액 (bet 또는 win)이 '거래'테이블에 있습니까? 즉, 거래가 베팅인지 아니면 둘 다인지 또는 둘 다인지 알아보기 위해'bet_transactions'와'win_transactions' 만 검색합니다. –