简体   繁体   English

从多个表中选择行

[英]Select row from multiple table

I am trying to get data from multiple tables (all tables are identical) 我正在尝试从多个表中获取数据(所有表都相同)

Select column1,column2 From table1,table2

Is it a correct way to get this in MYSQL. 这是在MYSQL中获取此信息的正确方法。 (I am passing table names dynamically in the query) (我在查询中动态传递表名)

The syntax you need is this one : 您需要的语法是以下一种:

SELECT a.column1, b.column2
FROM table1 AS a, table2 AS b

AS give your table an alias you can directly use in your request. AS给您的表一个别名,您可以直接在您的请求中使用它。

Like: 喜欢:

SELECT table1.column1, table2.column2 
FROM table1, table2

By fully-qualifying the column using the table, you won't run into conflicts when columns have the same names in the tables. 通过使用表对列进行完全限定,当表中的列具有相同的名称时,您就不会发生冲突。 As zessx states in his answer, an alternate way to do this is to alias the tables, which can make the query simpler with long table names / schema names. 正如zessx在他的回答中指出的那样,执行此操作的另一种方法是为表加上别名,这样可以使用长表名/模式名简化查询。

Maybe this: 也许这样:

 SELECT column1, column2
 FROM table1
 UNION ALL
 SELECT column1, column2
 FROM table2

But you cannot pass in table names dynamically. 但是您不能动态传递表名。 They must be known to the database in advance (so you need to construct the SQL in your program first). 它们必须事先为数据库所知(因此,您需要首先在程序中构造SQL)。

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

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