select 来自许多 table 的同一列
select same column from many table
如何使用 sql 和 php 得到这样的结果?
table 1 name = "T1"
column = "ip"
rows = "ip1,ip2,ip3,ip4"
,
table 2 name = "T2"
column = "ip"
rows = "ip5,ip6,ip7,ip8"
,
table 3 name = "T3"
column = "ip"
rows = "ip9,ip10"
所有 table 中的列名称相同
并且所有 table 都在同一个数据库中
我想从这三个 tables 得到这个输出:
"T1" => "ip1,ip2,ip3,ip4",
"T2" => "ip5,ip6,ip7,ip8",
"T3" => "ip9,ip10"
我可以用多个查询来做到这一点,但我只想用一个查询来做到这一点!
请帮忙
如果列名和计数匹配,您可以像这样进行联合或全部联合
SELECT "Table1" as TableName, column1, column2, column3
FROM Table1
UNION
SELECT "Table2" as TableName, column1, column2, column3
FROM Table2
UNION
SELECT "Table3" as TableName, column1, column2, column3
FROM Table3
并且你需要在你的 PHP 代码中有一些逻辑来按第一列对行进行分组,即。 Table 姓名>
希望对您有所帮助。
如何使用 sql 和 php 得到这样的结果?
table 1 name = "T1"
column = "ip"
rows = "ip1,ip2,ip3,ip4"
,
table 2 name = "T2"
column = "ip"
rows = "ip5,ip6,ip7,ip8"
,
table 3 name = "T3"
column = "ip"
rows = "ip9,ip10"
所有 table 中的列名称相同
并且所有 table 都在同一个数据库中
我想从这三个 tables 得到这个输出:
"T1" => "ip1,ip2,ip3,ip4",
"T2" => "ip5,ip6,ip7,ip8",
"T3" => "ip9,ip10"
我可以用多个查询来做到这一点,但我只想用一个查询来做到这一点!
请帮忙
如果列名和计数匹配,您可以像这样进行联合或全部联合
SELECT "Table1" as TableName, column1, column2, column3
FROM Table1
UNION
SELECT "Table2" as TableName, column1, column2, column3
FROM Table2
UNION
SELECT "Table3" as TableName, column1, column2, column3
FROM Table3
并且你需要在你的 PHP 代码中有一些逻辑来按第一列对行进行分组,即。 Table 姓名>
希望对您有所帮助。