简体   繁体   English

mysql基于第三个id创建两个表的视图

[英]mysql create view of two tables based on id from third

I have found a lot of similar posts about merging two tables based on an id from a third, however I just can't seem to work out the logic. 我发现了很多类似的文章,它们基于第三个ID合并两个表,但是我似乎无法弄清楚其逻辑。

Give the three tables 给出三个表

tableA

| uuid |  ttl  |  ord |
-----------------------
| alpha|  Alp  |     1|
| beta|  Bet  |     2|
| gamma|  Gam  |     3|

tableB

| uuid |  ttl  |  ord | tab_A_ref
--------------------------------
| joe  |  Jo  |     1|  alpha
| mike |  Mi  |     2|  beta
| peter|  Pe  |     3|  alpha
| alan |  Pe  |     4|  beta
| tom  |  Pe  |     5|  gamma

tableC

| uuid |  ttl  |  ord | tab_A_ref
--------------------------------
| jane  |  Ja  |     1|  alpha
| marg  |  Ma  |     2|  beta
| phobe |  Ph  |     3|  alpha
| anon  |  An  |     4|  beta
| toni  |  To  |     5|  gamma

I am looking to create a view myView where tab_A_ref = alpha or beta 我想创建一个视图myView ,其中tab_A_ref = alpha or beta

| uuid  |  ttl  | ord | tab_A_ref
| joe   |  Jo   |     1|  alpha
| peter |  Pe   |     3|  alpha
| jane  |  Ja   |     1|  alpha
| phobe |  Ph   |     3|  alpha
| mike  |  Mi   |     2|  beta
| alan  |  Pe   |     4|  beta
| marg  |  Ma   |     2|  beta
| anon  |  An   |     4|  beta

using the a basic join, 使用基本联接,

SELECT
  tableB.uuid, tableB.ttl, tableB.ord, tableB.tab_A_ref  
  FROM tableB
  INNER JOIN tableA on tableB.tab_A_ref = tableA.uuid 

I extended the logic to merge the two 我扩展了合并两者的逻辑

SELECT
      tableB.uuid, tableB.ttl, tableB.ord, tableB.tab_A_ref,
      tableC.uuid, tableC.ttl, tableC.ord, tableC.tab_A_ref,  
FROM 
      tableB INNER JOIN tableA on tableB.tab_A_ref = tableA.uuid, 
      tableC INNER JOIN tableA on tableC.tab_A_ref = tableA.uuid

but that generates an error not unique table/alias: tableA 但这会产生错误, not unique table/alias: tableA

From the other posts it looks like I should have a nested bracketed JOIN, having tried a number of combinations all fail. 从其他帖子看来,我应该有一个嵌套的bracketed JOIN,尝试了多种组合都失败了。 So how do I format the second JOIN ? 那么如何格式化第二个JOIN

All tables (temporary, subqueries, physical or otherwise) must be uniquely named in the query. 所有表(临时表,子查询,物理表或其他表)必须在查询中唯一地命名。

Otherwise the database engine has no idea which source of data you're referring to. 否则,数据库引擎不知道您要指代哪个数据源。

Your original: 您的原件:

SELECT
      tableB.uuid, tableB.ttl, tableB.ord, tableB.tab_A_ref,
      tableC.uuid, tableC.ttl, tableC.ord, tableC.tab_A_ref,  
FROM 
      tableB INNER JOIN tableA on tableB.tab_A_ref = tableA.uuid, 
      tableC INNER JOIN tableA on tableC.tab_A_ref = tableA.uuid

In the above, how does the query engine know which tableA to use? 在上面,查询引擎如何知道要使用哪个tableA You've declared it twice. 您已经声明了两次。 Remember you're applying a filter ( ON tableB.tab_A_ref = tableA.uuid ) - so you've got two result-sets (one from the first join, a different one from the second) and they're both referred to as tableA . 记住,您正在应用一个过滤器( ON tableB.tab_A_ref = tableA.uuid )-因此,您有两个结果集(一个来自第一个ON tableB.tab_A_ref = tableA.uuid ,另一个不同于第二个ON tableB.tab_A_ref = tableA.uuid ),它们都被称为tableA

Simply, use an alias ( AS ) to uniquely reference all tables (joined or otherwise): 只需使用别名( AS )即可唯一地引用所有表(联接表或其他表):

SELECT
      tableB.uuid, tableB.ttl, tableB.ord, tableB.tab_A_ref,
      tableC.uuid, tableC.ttl, tableC.ord, tableC.tab_A_ref,  
FROM 
      tableB INNER JOIN tableA AS a_ref ON tableB.tab_A_ref = a_ref.uuid, 
      tableC INNER JOIN tableA AS b_ref ON tableC.tab_A_ref = b_ref.uuid

The following JOIN logic is incorrect: 以下JOIN逻辑不正确:

  tableB INNER JOIN tableA on tableB.tab_A_ref = tableA.uuid, 
  tableC INNER JOIN tableA on tableC.tab_A_ref = tableA.uuid

The query should be: 查询应为:

SELECT
    tableB.uuid, tableB.ttl, tableB.ord, tableB.tab_A_ref  
FROM tableB
    INNER JOIN tableA 
            ON tableB.tab_A_ref = tableA.uuid 
    INNER JOIN tableC 
            ON tableC.tab_A_ref = tableA.uuid 


The essence of the logic used here is that the first JOIN already creates a virtual unit that can be joined with any other tables as necessary. 这里使用的逻辑的本质是,第一个JOIN已经创建了一个虚拟单元 ,可以根据需要将其与任何其他表连接。 tableA is already partaking in that JOIN and unless there's some special need (which doesn't seem to be the case here), you don't need to specify it again in the query. tableA已经在该JOIN中发挥作用,除非有特殊需要(在这里似乎不是这种情况),否则您无需在查询中再次指定它。

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

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