Home > database >  Everyone a great god genuflect is begged! Questions about the mysql query, ask everybody to help sol
Everyone a great god genuflect is begged! Questions about the mysql query, ask everybody to help sol

Time:10-11

Business needs, I want to statistical oneself peacetime card usage, set up two tables,


Table 1 (for card based condition)

fields:

Id card number, bank, lines

table 2: (used for card for daily use, spending and reimbursement)


fields:

Id (the primary key, corresponding to the id of the table 1), the usage (negative Numbers represent spending, positive number represents repayment)


Want the end result,






CodePudding user response:

I hope you grant instruction! Best give a complete SQL statements, has been studying for two days, not fix

CodePudding user response:


Select t1. Banks, t1. The card number, t1), sum (t2) usage) * (1) as the use of lines, (sum (t2) usage) + t1. Lines) as the remaining amount from t2
Inner join t1 on t2. Id=t1. Id
Group by t2. Id

CodePudding user response:

The select t1. Banks, t1. The card number,
The sum (t1) lines) as lines,
The sum (t1) lines) + sum (t2) usage) as the rest of the lines,
The usage of the sum (t2)) * 1 as limit
The from t1
Left the join t2 on tl. Id=t2. Id
Group by t1. Banks, t1. The card number
  • Related