简体   繁体   English

MySQL多列联接与重合数据

[英]mySql multi-column join with coincident data

I have three tables. 我有三张桌子。 Each one has three columns. 每个都有三列。 It looks like this: 看起来像这样:

table 1                   table 2                    table 3
--------------------      --------------------       --------------------
col1    col2    colA      col1    col2    colB       col1    col2    colC
1       A       data      3       C       data       5       E       data
2       B       data      2       B       data       6       F       data
3       C       data      1       A       data       3       C       data
4       D       data      4       D       data       2       B       data
5       E       data      6       F       data       1       A       data
6       F       data      5       E       data       3       C       data

My question is if it is at all possible with JOINS to output something like this: 我的问题是,JOINS是否完全有可能输出如下内容:

output table                
-----------------------------------------
col1    col2    colA       colB      colC
1       A       data       data      data
2       B       data       data      data
3       C       data       data      data
4       D       data       data      data
5       E       data       data      data
6       F       data       data      data

Note that Col1 and Col2 always have the same values in the different columns but are in different order. 请注意,Col1和Col2在不同列中始终具有相同的值,但顺序不同。

I don't even know if this is possible but ideally the query would join the three tables and relate the information on col1 and col2 with each respective table and reorder the two joining tables and output a single table/array. 我什至不知道这是否可行,但理想情况下,查询将联接三个表,并将col1和col2的信息与每个表相关联,并对两个联接表重新排序并输出单个表/数组。

Let me know if this is really stupid or complex... I've been trying to wrap my head around this but by mySql knowledge is very limited. 让我知道这是真的还是愚蠢的...我一直在努力解决这个问题,但是mySql的知识非常有限。

SELECT *
FROM t1
LEFT JOIN t2 USING (col1, col2)
LEFT JOIN t3 USING (col1, col2)

Of course it's possible. 当然可以。 As long as you have the same data across the tables, you can link them and display the data together. 只要表中的数据相同,就可以链接它们并一起显示数据。

For instance: 例如:

SELECT table1.*, colB, colC FROM table1, table2, table3
WHERE table1.col1 = table2.col1 AND table2.col1 = table3.col1
ORDER BY table1.col1 ASC;

would output the table in your question. 将输出您问题中的表格。 I didn't use col2 as a condition in the example because it wasn't needed to link the data together. 在示例中,我没有使用col2作为条件,因为不需要将数据链接在一起。

Here is SQL for a simple double-join. 这是用于简单双连接的SQL。 Tweakable if you need to clarify anything I misunderstood. 如果您需要澄清我误解的任何内容,可以调整。

SELECT 
  table1.col1, 
  table1.col2, 
  colA, 
  colB, 
  colC
FROM table1 
JOIN table2 
  ON table1.col1 = table1.col1 
  AND table1.col2 = table2.col2
JOIN table3 
  ON table1.col1 = table3.col1 
  AND table1.col2 = table3.col2

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

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