简体   繁体   English

MYSQL联合与存在声明

[英]MYsql union with exists statement

So i've got a fairly complicated sql statement merging ten tables into one table based on requirements. 因此,我有一个相当复杂的sql语句,可根据需求将十张表合并为一张表。

A sample of the results (only consists of the essential required for this question) 结果样本(仅包含此问题所需的基本内容)

Unique | date | amount | Table
12345  | 10/2 | 200    |   1
12345  | 10/2 |  20    |   2
23456  | 10/3 | 100    |   3

My question is, since the unique is the same for the first 2 rows, but from different tables, how can i merge them together so it becomes this? 我的问题是,由于前两行的唯一性相同,但是来自不同的表,我如何将它们合并在一起呢?

Unique | date | amount | Table
12345  | 10/2 | 220    |   1 (Don't really care about the table number)
23456  | 10/3 | 100    |   3

This is a sample of my statement 这是我的陈述的一个例子

(SELECT unique, dates, amt, tbl FROM ..... WHERE ....) 
UNION 
(SELECT unique, dates, amt, tbl FROM ..... WHERE ....) ORDER BY dates ASC

Would be great if it could be done via MYsql, if not, answers in PHP / VB.net would also be accepted. 如果可以通过MYsql完成,那就太好了,否则,PHP / VB.net中的答案也将被接受。 All help appreciated. 所有帮助表示赞赏。 Thanks 谢谢

You could wrap your query with a group by using a group_concat to grab all the table names. 您可以使用group_concat来获取所有表名,从而用组将查询包装起来。 For example 例如

DROP TABLE IF EXISTS T1,T2,T3;

CREATE TABLE T1(UNIQUEID  INT, DT DATE, AMT INT);
CREATE TABLE T2(UNIQUEID  INT, DT DATE, AMT INT);
CREATE TABLE T3(UNIQUEID  INT, DT DATE, AMT INT);

INSERT INTO T1 VALUES(1,'2017-01-01',10),(1,'2017-02-01',10),(2,'2017-01-01',20);
INSERT INTO T2 VALUES(1,'2017-01-01',10),(3,'2017-01-01',10),(4,'2017-01-01',20);
INSERT INTO T3 VALUES(2,'2017-01-01',10),(5,'2017-01-01',10),(4,'2017-01-01',20);

SELECT  UNIQUEID,DT,SUM(AMT) SUMAMT,SUBSTR(GROUP_CONCAT(TNAME),1,2)
FROM
(
SELECT 'T1' TNAME, UNIQUEID, DT,AMT FROM T1
UNION ALL
SELECT 'T2' TNAME, UNIQUEID, DT,AMT FROM T2
UNION ALL
SELECT 'T3' TNAME, UNIQUEID, DT,AMT FROM T3
) S
GROUP BY UNIQUEID,DT;

Result 结果

+----------+------------+--------+---------------------------------+
| UNIQUEID | DT         | SUMAMT | SUBSTR(GROUP_CONCAT(TNAME),1,2) |
+----------+------------+--------+---------------------------------+
|        1 | 2017-01-01 |     20 | T1                              |
|        1 | 2017-02-01 |     10 | T1                              |
|        2 | 2017-01-01 |     30 | T1                              |
|        3 | 2017-01-01 |     10 | T2                              |
|        4 | 2017-01-01 |     40 | T3                              |
|        5 | 2017-01-01 |     10 | T3                              |
+----------+------------+--------+---------------------------------+
6 rows in set (0.00 sec)

The Group_concat can be ordered if you wish and if you leave out the substring then you could print all table names. 如果需要,可以对Group_concat进行排序,如果省略子字符串,则可以打印所有表名。 It's not worth using a union statement without the all clause since mysql will attempt to dedupe and you don't really want to. 不使用带有all子句的union语句是不值得的,因为mysql会尝试进行重复数据删除,而您实际上并不想这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM