How to select a table name in a SQL query?

I use UNION and LIMIT to select the earliest occurrence of a row such as a table from several tables. I need a record which table satisfied the query in the result set.

Is there a way to do something like:

SELECT id, someField, tableName FROM someUnknownTable WHERE someConditions = true

+4
source share
1 answer

You can select tableName as a constant value:

 Select id, someField, 'Table1' As tableName From table1 Union Select id, someField, 'Table2' As tableName From table2 

The second alias ( As tableName ) can be omitted.

+2
source

All Articles