Home > database >  The MySQL table more LianZha and sum calculation
The MySQL table more LianZha and sum calculation

Time:10-04

Is such a situation there are three tables are respectively the user user, trade orders, and money record user_money_record
Now according to the user to count their order total amount and account balance and total amount of top-up

User table
Id realname money
1 piece of xx 100
Wang xx 500
23 li xx 1500

The trade
Id uid realmoney
1 2 5000
2 1 1000
3 2 5000
4 3 10000
5 1 3500

User_money_record
Id uid money
1 2 5000
2 1 5000
3 2 2000
4 2 3000
5 3 5000
June 3, 5000

Table structure and data below
Statistics they order total amount and account balance and total amount of top-up

CodePudding user response:

Order total amount and the account balance and top-up respectively corresponding to the total amount which table which field

CodePudding user response:

reference 1st floor wangjian0228 response:
order total amount and the account balance and top-up respectively corresponding to the total amount of which table which field


It says clear oh user corresponding to the user table was the trade order user_money_record corresponding user prepaid phone records

CodePudding user response:

The select Anderson d, A.r ealname, sum (B.r ealmoney), sum (c. oney)
The from user A, trade B, user_money_record C
Where Anderson, d=B.u id and Anderson, d=C.u id
Group by Anderson, d

CodePudding user response:

The
reference 3 floor rucypli response:
select Anderson d, A.r ealname, sum (B.r ealmoney), sum (c. oney)
The from user A, trade B, user_money_record C
Where Anderson, d=B.u id and Anderson, d=C.u id
Group by Anderson d

He had a balance, this can't do this

CodePudding user response:

The
reference 3 floor rucypli response:
select Anderson d, A.r ealname, sum (B.r ealmoney), sum (c. oney)
The from user A, trade B, user_money_record C
Where Anderson, d=B.u id and Anderson, d=C.u id
Group by Anderson d


No, oh so there will be a repeat of calculation

CodePudding user response:


 select *,(select sum (realmoney) from the trade where uid=u.i d), 
(select sum (money) from user_money_record where uid=u.i d)
From the user table u

CodePudding user response:

refer to 6th floor ACMAIN_CHM response:
 select *, 
(select sum (realmoney) from the trade where uid=u.i d),
(select sum (money) from user_money_record where uid=u.i d)
From the user table u


There is a
 select u.r ealname, u.m oney, 
(select sum (realmoney) from trade_history where uid=u.i d) as totalMoney,
(select sum (money) from user_money_record where uid=u.i d) as allMoney
The from user u

CodePudding user response:

refer to 6th floor ACMAIN_CHM response:
 select *, 
(select sum (realmoney) from the trade where uid=u.i d),
(select sum (money) from user_money_record where uid=u.i d)
From the user table u


In the wrong is the
 select u.n ame, t.r ealmoney, umr. Money from the user u 
Left the join
(select the uid, sum (realmoney) as realmoney from trade group by uid) t on t.u id=u.i d
Left the join
(select uid, sum (money) as money from user_money_record group by uid) umr on umr) uid=uid

CodePudding user response:


 select *,(select sum (realmoney) from the trade where uid=u.i d), 
(select sum (money) from user_money_record where uid=u.i d)
From the user table u

CodePudding user response:

Agreed to advice on the 8th floor,

CodePudding user response:

Built user table have a problem, you didn't also description fields and needs corresponding relation

CodePudding user response:

The user list in the money represent? The balance?

CodePudding user response:

Select sum (id) from (
Select count (nr. Goods_id) as id from ncp_release nr
UNION ALL
Select count (pr) goods_id) as id from pesticide_release pr
UNION ALL
Select count (fr. Goods_id) as id from fertilizer_release fr
B) the as
  • Related