简体   繁体   English

查询两个表-但不尝试联接?

[英]Query Two Tables - but not trying to JOIN?

I have two tables that almost have identical columns. 我有两个表几乎具有相同的列。 The first table contains the "current" state of a particular record and the second table contains all the previous stats of that records (it's a history table). 第一个表包含特定记录的“当前”状态,第二个表包含该记录的所有先前状态(这是一个历史记录表)。 The second table has a FK to the first table. 第二个表具有到第一个表的FK。

I'd like to query both tables so I get the entire records history, including its current state in one result. 我想查询两个表,以便获得整个记录的历史记录,包括一个结果中的当前状态。 I don't think a JOIN is what I'm trying to do as that "joins" multiple tables "horizontally" (one or more columns of one table combined with one or more columns of another table to produce a result that includes columns from both tables). 我不认为JOIN是我要执行的操作,因为它“水平”“连接”了多个表(一个表的一个或多个列与另一表的一个或多个列组合以产生包含来自以下列的结果两个表)。 Rather, I'm trying to "join"(???) the tables "vertically" (meaning, no columns are getting added to the result, just that the results from both tables are falling under the same columns in the result set). 而是,我试图“垂直”地“连接”(???)表(意味着,没有列被添加到结果中,只是两个表的结果都落在结果集中的同一列下) 。

Not exactly sure if what I'm expressing make sense -- or if it's possible in MySQL. 不确定我要表达的内容是否有意义-或在MySQL中是否可行。

UNION. 联盟。

Select colA, colB From TblA

UNION

Select colA, colB From TblB

To accomplish this, you could use a UNION between two SELECT statements. 为此,可以在两个SELECT语句之间使用UNION I would also suggest selecting from a derived table in the following manner so that you can sort by columns in your result set. 我还建议以以下方式从派生表中进行选择,以便您可以按结果集中的列进行排序。 Suppose we wanted to combine results from the following two queries: 假设我们要合并以下两个查询的结果:

SELECT FieldA, FieldB FROM table1;
SELECT FieldX, FieldY FROM table2;

We could join these with a UNION statement as follows: 我们可以将它们与UNION语句一起加入,如下所示:

SELECT Field1, Field2 FROM (
    SELECT FieldA AS `Field1`, FieldB AS `Field2` FROM table1 
    UNION SELECT FieldX AS `Field1`, FieldY AS `Field2` FROM table2) 
AS `derived_table` 
ORDER BY Field1 ASC, Field2 DESC

In this example, I have selected from table1 and table2 fields which are similar, but not identically named, sharing the same data type. 在此示例中,我从table1table2字段中选择了相似但名称不同的字段,它们共享相同的数据类型。 They are matched up using aliases ( eg , FieldA in table1 and FieldX in table2 both map to Field1 in the result set, etc. ). 他们匹配了使用别名( 例如FieldAtable1FieldXtable2两种地图Field1的结果集, 等等 )。

If each table has the same column names, field aliasing is not required, and the query becomes simpler. 如果每个表具有相同的列名,则不需要字段别名,并且查询变得更简单。


Note: In MySQL it is necessary to name derived tables, even if the name given is not intended to be used. 注意:在MySQL中,即使不打算使用给定的名称,也必须命名派生表。

Your after a left join on the first table. 您在第一个桌子上左加入后。 That will make the right side I'd he their a number (exists in both) or null (exists only in the left table ) 这将使我可以在右侧输入一个数字(两者都存在)或null(仅存在于左表中)

You want 你要

select lhs.* , rhs.id from lhs left join  rhs using(Id) 

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

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