我需要从数据库表中获取所有列名。如果它是单个表,我可以使用:
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='nameofschema'
AND `TABLE_NAME`='tbl_name'
GROUP BY `COLUMN_NAME`
现在我需要从十个表中获取列名并一次显示列名。 我怎样才能做到这一点? 请帮我解决这个问题。
答案 0 :(得分:1)
像这样尝试IN()
运算符
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='nameofschema'
AND `TABLE_NAME` IN ('tbl_name','tbl_name2','tbl_name3')
GROUP BY `COLUMN_NAME`
如果表位于不同的模式中,则可以使用AND,OR opeartors
的组合SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE (`TABLE_SCHEMA`='nameofschema' AND `TABLE_NAME` = 'tbl_name')
OR
(`TABLE_SCHEMA`='nameofschema1' AND `TABLE_NAME` = 'tbl_name1')
GROUP BY `COLUMN_NAME`
答案 1 :(得分:1)
您可以使用IN()
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='dbname'
AND `TABLE_NAME` IN ('table1' ,'table2'.....)
GROUP BY `COLUMN_NAME`
或者显示数据库中的所有列
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='dbname'
GROUP BY `COLUMN_NAME`
答案 2 :(得分:1)
尝试
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='nameofschema'
AND `TABLE_NAME` in ('tbl_name1','tbl_name2','tbl_name3')
GROUP BY `COLUMN_NAME`
答案 3 :(得分:1)
试试这个
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;