select same column from many table

sajjad rast :

how i can get a result like this with sql and 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"

columns name in all table is same

and all tables is in a same database

i want get this output from these three tables:

"T1" => "ip1,ip2,ip3,ip4",
"T2" => "ip5,ip6,ip7,ip8",
"T3" => "ip9,ip10"

i can do this with several query but i want do this with just one query!

please help

Aryan :

If columns names and count are matching you can do union or union all like this

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

And The you need to have some logic in your PHP code to do group the rows by first column ie. Table Name>

I hope this helps.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=402244&siteId=1