Data as shown above, the desired result is that the grouping by id, and then judge condition is lend_time, if there is a value lend_time, then sum, according to the principal and if lend_time is empty, the display is 0, but the problem is with id 1 lend_time only one, principal has two, show and I can only display 1000, rather than 6000 (1000 + 5000), which means there are lend_time value summation, when no lend_time belong to a value of 0, beg god for help,
CodePudding user response:
Suggest you list your table structure, and provide the test data, and based on these test data that corresponds to the correct result,Refer to the way the post questions http://bbs.csdn.net/topics/320211382
1. You create table XXX.. Statement
2. You insert into XXX... Statement
3. The result is what kind, and give a simple algorithm description)
4. Do you use the database name and version of (often asked in MS SQL server edition MySQL)
So people who want to help you to build the same environment with you directly, and in providing the test before, avoid the error in the text understanding,
CodePudding user response:
Select id, the sum (the principal) from TB where id in (select id from TB where lend_time is not null)CodePudding user response:
SELECT the IF (ISNULL (a), 0, b) AS the FROM b (
SELECT SUM (UNIX_TIMESTAMP (lend_time)) AS a, the SUM (principal) AS b
FROM the test
GROUP BY id
) AS t
Is probably so, actually very simple, is your problem set said not clear
CodePudding user response: