[英]Merge two tables with same data structure then join to another table
The two tables with the same data structure: 这两个表具有相同的数据结构:
issues: old_issues:
|id|itemno|issuedate | |id|itemno|issuedate |
|1 |200 |2018-02-01| |1 |200 |2017-12-21|
|2 |300 |2018-02-03| |2 |200 |2017-12-20|
|3 |200 |2018-02-02| |3 |400 |2016-05-09|
|4 |500 |2016-07-10|
I want to merge these two tables into a new_issues
table, then I want to join it to another table that looks like this: 我想将这两个表合并为一个
new_issues
表,然后将其连接到另一个如下表:
items:
|itemnumber|location|ccode|
|200 |DEP |ARACD|
|300 |DEP |ARACD|
|400 |SYD |ARACD|
|500 |DEP |DVD |
But I only want to select the items
with a query like this: 但是我只想通过如下查询选择
items
:
SELECT * FROM items WHERE location = 'DEP' AND ccode = 'ARACD'
And select the new_issues
that meet this criteria: 并选择符合以下条件的
new_issues
:
SELECT * FROM new_issues WHERE issuedate < '2018-01-01'
This is what I have tried, but it seems to hang there loading forever as there are millions of rows in the issues tables: 这是我尝试过的方法,但是由于问题表中有数百万行,因此它似乎永远挂在那里加载:
SELECT i.itemno, new_issues.issuedate
FROM (SELECT * FROM issues UNION SELECT * FROM old_issues) AS new_issues
RIGHT JOIN items i ON i.itemno = new_issues.itemno
WHERE i.ccode = 'ARACD' AND i.location = 'DEP' AND new_issues.issuedate < '2018-01-01'
Not sure if this is what you're looking for but have you tried a UNION SELECT
? 不知道这是您要找的内容,但是您尝试过
UNION SELECT
吗?
SELECT * FROM newitems WHERE location = 'DEP' AND ccode = 'ARACD'
UNION SELECT * FROM olditems WHERE location = 'DEP' AND ccode = 'ARACD' GROUP BY itemno
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.