比较两个表后如何输出匹配项? [英] How do I output a match after comparing two tables?
问题描述
我想比较两个表. 但是我不知道该怎么办,因为我不够好. 我想知道如何编写查询
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
1号桌
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
2号表
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
想要的结果
条件和顺序如下.
-
1号表,对每个chart_num具有相同值的列求和并访问.
no.1_table, sum a column with the same value of each chart_num and visit.
比较1号结果值的访问和card_count_received与2号表的card_date和advenced_amount值.
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.
如果chart_num的值和no.1_table的访问值与no.2_table的card_date和no.2_table的added_count值相同,则否则输出错误并进行空处理.
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.
如何创建查询语句?
数据库技能不足.请给我答复.
Database skills are insufficient. Please give me a reply.
推荐答案
- 1号表,对每个chart_num具有相同值的列求和并访问.
为此,您要 group by
chart_num并访问.任何具有相同chart_num和visit的行将在结果中显示为单行.然后,您可以 sum
收到的金额,这将为一个小组.
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
是个问题.您无法显示它,因为它不是group by
的一部分.这是一个字符串,因此将其与sum
或count
之类的函数进行聚合没有任何意义.尽管数据中的chart_num具有相同的chart_name,但这并不能保证.
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.
一种解决方案是使用 group_concat
将组中的每个名称连接在一起.每个组只能有一个名字.
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
但是,正确的解决方案是修复架构. chart_name是重复的,这是要避免的.而是将图表列移动到自己的表中.然后,要获取图表名称,请加入chart_num.
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
- 将第1个结果值的访问和card_count_receive与card_date和no.2_table的advanced_amount值进行比较.
我们需要左连接,第二个表将card_date与访问进行匹配. 左联接表示左"表中的所有行(即from
表)将始终显示,即使联接表中没有匹配项.
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.
访问是一个日期. card_date不是日期,而是时间戳.为了匹配它们,我们需要将card_date
转换为日期.
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
如果chart_num和no.1_table的访问值与no.2_table的card_date和no.2_table的added_count值相同,则否则输出错误并进行空处理.
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.
我们需要将advanced_amount与sum(card_amount_received)进行比较.如果它们相等:好的.如果不是:错误.在标准SQL中,我们将使用 case
,但是MariaDB具有非标准 if
,它更加紧凑.
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
这篇关于比较两个表后如何输出匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!