Getting table name with the select query of the same table - mysql

I am trying to get the table name within the sql query that I am writing to get data from the same table, for example
Table_one
TABLE_TWO
So while doing this query Select * from table_one UNION Select * from table_two
the result that i get is the data of two tables in one table like this:
But what I need that one more column to come in the table to see the data is coming from which table as following

Use union all:
select id, name, age, 'table_one' as tabel_name
from table_one
union all
select id, name, age, 'table_two'
from table_two

Related

Select the same columns from a list of tables

I am looking to run this query on a list of tables.
SELECT Description,Code,count(*) as count
FROM table1
group by Description,code
having count(*) > 1
I will have to run this query on 30+ different tables, I was wondering If I could change the from statement and just list off the table names.
In addition, is there some functionality that will add the name of the table that it came from in a seperate column to distinguish where the results came from?
Thanks in advance
You might use UNION ALL to put it together. Unless you need some dynamic table selection.
SELECT Description,Code,count(*) as count, 'table1' as tableNane
FROM table1
group by Description,code
having count(*) > 1
UNION ALL
SELECT Description,Code,count(*) as count, 'table2' as tableNane
FROM table2
group by Description,code
having count(*) > 1
...
Actualy I like #Shubhradeep Majumdar version. It will generate more concise code.
SELECT Description,Code, Count(Code), tableName FROM (
SELECT Description,Code, 'table1' as tableName
FROM table1
UNION ALL
SELECT Description,Code, 'table2' as tableName
FROM table2
) tables
GROUP BY tableName, Description, Code
HAVING COUNT(Code) > 1
But there might be a little catch to it. It is more elegant code, but it might actually be slower than first version. The problem is that tableName is appended at every record before grouping while in my first version you do that on already processed data.
Carrying over from #Marek's answer, You could first append all the tables to a table with union all.
select *, 'tab1' as tabnm from tab1
union all
select *, 'tab2' as tabnm from tab2
union all
select *, 'tab3' as tabnm from tab3
-- and so on...
And then use your code to process that final table.
will save you a great deal of time.
EDITED with a column specifying the table name

Excluding column from MySQL SELECT distinct query with UNION across tables

I have an SQL database with several tables of patient data. Every table has one column in common, an ID number representing each patient. There is significant overlap between the tables, i.e. the same patient ID number often appears on multiple tables. What I would like to do is SELECT all distinct patient ID numbers that do not appear on one specific table.
You can use UNION and NOT IN like this:
select id
from (
select id from table1
union
select id from table2
union
select id from table3
...
) t where id not in (
select id from sometable
);

Retrieve records on matching a field in any 2 tables out of 4 tables

I have four tables with same fields. Now I want to join these tables in such a way that I retrieve records only if there is a match between any two tables on a field(like name).
Thanks in advance.
This would return all the name values that appear in more than one table:
select
name
from
(select distinct
name
from table1
union all
select distinct
name
from table2
union all
select distinct
name
from table3
union all
select distinct
name
from table4) temp
group by name
having count(*) > 1;
Check out the interactive example.

can a table be selected in an sql query to group the result

hello i just wanted to know is it possible to select the table name in an sql query as part of your result
so for example when using UNION to join 2 tables can you specify where each result is coming from because so far i have had to add an extra column called type to each table to specify and it just seems there could be a better way
SELECT id, name, type
FROM table1
UNION ALL
SELECT id, name, type
FROM table2
LIMIT 20`
result =
id, name, type
id, name, type
but i want to still have the type/table name without selecting it or even having it in my table
pleas let me know if this is possible or if this is the only way to do it ether way thanks in advance
Try this:
SELECT id, name, type, 'table1' AS FromWhichTable
FROM table1
UNION ALL
SELECT id, name, type, 'table2'
FROM table2
LIMIT 20

Create table as ( using a column and table name)

I need to build a table from several tables in MySql and I want it has two columns like:
-------------------
name | table_name |
-------------------
I'm doing this:
Create table teams as
(
Select name from table1
union
Select name from table2
union
Select name from table3);
How could I include each table name as a second column??
Thank you!
Include a quoted string literal. Be sure to also give it a column alias, which will be used as the column name in the resultant table.
CREATE TABLE teams AS
/* Quoted string literal with column alias */
(SELECT name, 'table1' AS `tablename` FROM table1)
UNION
(SELECT name, 'table2' AS `tablename` FROM table2)
UNION
(SELECT name, 'table3' AS `tablename` FROM table3);
Note that since you are in effect adding a second value to each row which differentiates it from potentially similar rows in the other tables, the UNION is now the eqivalent of a UNION ALL, and duplicate rows won't be de-duped as the plan UNION would have. Just beware, that the results may differ from what your original UNION produced.