How do I get the column name of two tables in a single query ?
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
where table_name = 'table_name';
This works for single table. But if I try
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
where table_name = 'table1'
AND
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
where table_name = 'table2';
This throws error.
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS where table_name = 'table2' ' at line 5
Error Code: ER_PARSE_ERROR
2
Answers
if you want to select 2 columns from different tables, you need to add some relation between them.
On the basis of that relationship, you can fetch data from two different tables.
Please check the link: https://www.w3schools.com/mysql/mysql_join.asp
Hope it will solve your problem.
Different queries will be put together using
UNION ALL
(orUNION
only if it’s necessary to exclude identic values which appear in both queries):Since you want to get data from the same table, you don’t need two queries at all.
Just use an
IN
clause……or use
OR
: