问题
I want to compare two tables. But I don't know what to do because I'm not good enough. I want to know how to write query
no.1_table
id chart_num chart_name visit card_amount_received
1 4 user1 2020-04-05 1000
2 5 user2 2020-05-05 1000
3 5 user2 2020-05-05 1000
4 5 user2 2020-06-05 1000
5 6 user3 2020-07-05 1000
6 6 user3 2020-08-05 1000
7 7 user4 2020-09-05 1000
8 7 user4 2020-09-05 1000
no.2_table
id card_date advenced_amount
1 2020-04-05 17:28:00 1000
2 2020-05-05 12:12:12 2000
10 2020-11-05 12:12:12 5000
want result
The conditions and order are as follows.
no.1_table, sum a column with the same value of each chart_num and visit.
Compare the visit and card_count_received of the result value of number 1 with the card_date and the advenced_amount values of no.2_table.
If the value of chart_num and visit of no.1_table is the same as the card_date of no.2_table and the added_count value of no.2_table, ok otherwise output error with null processing.
How do I create a query statement?
Database skills are insufficient. Please give me a reply.
回答1:
- no.1_table, sum a column with the same value of each chart_num and visit.
For this you want to group by the chart_num and visit. Any rows with the same chart_num and visit will appear as a single row in the result. Then you can sum the amount received, this will add up all the values for a group.
select
chart_num,
visit,
sum(card_amount_received) as card_amount_received
from table1
group by chart_num, visit
chart_name is a problem. You can't display it as it is not part of the group by. It's a string so it doesn't make sense to aggregate it with functions like sum or count. Though in the data chart_num has the same chart_name, that is not guaranteed.
One solution is to use group_concat to concatenate each name in a group together. There should only be one name per group.
select
chart_num,
visit,
group_concat(chart_name) as chart_name,
sum(card_amount_received) as card_amount_received
from table1
group by chart_num, visit
However, the proper solution is fix the schema. chart_name is duplicated, and that's to be avoided. Instead, move the chart columns into their own table. Then to get the chart name, join on the chart_num.
create table charts (
id serial primary key,
name varchar(255) not null
);
insert into charts (id, name) values
(4, 'user1'), (5, 'user2'), (6, 'user3'), (7, 'user4');
alter table table1 drop column chart_name;
select
charts.id as chart_num,
visit,
charts.name as chart_name,
sum(card_amount_received) as card_amount_received
from table1
join charts on charts.id = chart_num
group by chart_num, visit
- Compare the visit and card_count_received of the result value of number 1 with the card_date and the advanced_amount values of no.2_table.
We need a left join with the second table matching the card_date with the visit. A left join means all the rows in the "left" table (ie. the from table) will always appear even if there is no match in the join table.
visit is a date. card_date is not a date but a timestamp. To match them we'll need to convert card_date to a date.
select
charts.id as chart_num,
visit,
charts.name as chart_name,
sum(card_amount_received) as card_amount_received,
table2.card_date,
table2.advanced_amount as amount
from table1
join charts on charts.id = chart_num
left join table2 on date(table2.chart_date) = visit
group by chart_num, visit
If the value of chart_num and visit of no.1_table is the same as the card_date of no.2_table and the added_count value of no.2_table, ok otherwise output error with null processing.
We need to compare advanced_amount with sum(card_amount_received). If they're equal: ok. If not: error. In standard SQL we'd use a case, but MariaDB has a non-standard if that is much more compact.
select
charts.id as chart_num,
visit,
charts.name as chart_name,
sum(card_amount_received) as card_amount_received,
table2.card_date,
table2.advanced_amount as amount,
if(table2.advanced_amount = sum(card_amount_received), 'ok', 'error') as result
from table1
join charts on charts.id = chart_num
left join table2 on date(table2.chart_date) = visit
group by chart_num, visit
来源:https://stackoverflow.com/questions/61135166/how-do-i-output-a-match-after-comparing-two-tables