简体   繁体   English

MySQL Inner Join表基于列值

[英]MySQL Inner Join table based on column value

Suppose I have a table 'stats' with the following structure: 假设我有一个表'stats'具有以下结构:
tableName | id | pageViews
The tableName column corresponds to separate tables in the database. tableName列对应于数据库中的单独表。

When running a query against "stats", what would be the best way to inner join against the tableName column result to get each table's data? 在针对“stats”运行查询时,对于tableName列结果进行内连接的最佳方法是获取每个表的数据?
I was thinking of running dynamic selects in a foreach and then merging the results. 我想在foreach中运行动态选择然后合并结果。 Eg: 例如:

foreach($tableNames as $tableName) {
    $sql = "SELECT      *
            FROM        stats s
            INNER JOIN  $tableName tbl ON s.id = tbl.id
            WHERE       tableName = '$tableName'";
}

To have all tables' statistics, you can use a UNION, with 2 or more selects, one for each table: 要拥有所有表的统计信息,可以使用UNION,具有2个或更多选择,每个表一个:

( SELECT s.*
       , table1.title AS name      --or whatever field you want to show
  FROM stats s
    JOIN $tableName1 table1
      ON s.id = table1.id
  WHERE tableName = '$tableName1'
)
UNION ALL
( SELECT s.*
       , table2.name AS name      --or whatever field you want to show
  FROM stats s
    JOIN $tableName2 table2
      ON s.id = table2.id
  WHERE tableName = '$tableName2'
)
UNION ALL
( SELECT s.*
       , table3.lastname AS name      --or whatever field you want to show
  FROM stats s
    JOIN $tableName3 table3
      ON s.id = table3.id
  WHERE tableName = '$tableName3'
)
;

Using Winfred's idea with LEFT JOIN s. 使用Winfred与LEFT JOIN的想法。 It produces different results, eg every field from the other tables is output in it's own column (and many NULLs occur). 它会产生不同的结果,例如,其他表中的每个字段都在其自己的列中输出(并且会出现许多NULL)。

SELECT s.*
     , table1.title      --or whatever fields you want to show
     , table2.name
     , table3.lastname   --etc
FROM stats s
  LEFT JOIN $tableName1 table1
    ON s.id = table1.id
      AND s.tableName = '$tableName1'
  LEFT JOIN $tableName2 table2
    ON s.id = table2.id
      AND s.tableName = '$tableName2'
  LEFT JOIN $tableName3 table3
    ON s.id = table3.id
      AND s.tableName = '$tableName3'
--this is to ensure that omited tables statistics don't appear
WHERE s.tablename IN
   ( '$tableName1'
   , '$tableName2'
   , '$tableName3'
   )
;

Do you have the luxury to join all tables first,and process it afterwards? 您是否有奢侈品首先加入所有表格并在之后加工?

SELECT *
    FROM stats s
    LEFT OUTER JOIN tbl1 ON s.id = tbl.id
    LEFT OUTER JOIN tbl2 ON s.id = tbl2.id

Then you take the value you need in your program afterwards? 然后你在之后的程序中获得所需的价值?

You should try to minimise the number of queries you made to your database, try to do it in one go if possible. 您应该尽量减少对数据库进行的查询次数,尽可能尝试一次性完成。

Otherwise, think about Stored Procedures etc 否则,请考虑存储过程

This is one easy way of doing it(with overheads), Im sure others will help you out too. 这是一种简单的方法(带有管理费用),我相信其他人也会帮助你。

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

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