Create table as ( using a column and table name) - mysql

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.

Related

Getting table name with the select query of the same table

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

Selectively searching from a set of tables in MS Access

I wanted to know if it's possible to have one query which I could design so that it would take a certain record from only one table i.e. I have 3 tables and my query, based on a certain parameter would choose one table to take the records from.
You can do this if all tables have equal field names using a UNION query, and by adding a field that specifies from which table the row originated:
PARAMETERS ParamTableName VARCHAR(255);
SELECT * FROM (
SELECT *, "Table1" As TableName FROM Table1
UNION ALL
SELECT *, "Table2" As TableName FROM Table2
UNION ALL
SELECT *, "Table3" As TableName FROM Table3
) TheTables
WHERE TableName = ParamTableName

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
);

JOIN / UNION SELECT all rows from two tables with different number of columns

I am trying to join or union the results of two differently structured tables, order them by a their common pubdate column, but get an error message:
The used SELECT statements have a different number of columns
SELECT * FROM news WHERE published='1' AND image!='' AND featured='1'
JOIN
SELECT * FROM videos WHERE published='1'
ORDER BY pubdate DESC
How can I edit my query so that I can run my fetch array and retrieve the corresponding rows afterwards (the rows to be fetched is then decided on another common row that both tables share)?
You can UNION ALL tables with a different number of columns by adding the missing columns in the select list. I mean, suppose you have:
SQL> create table one ( c1 integer , c2 integer, c3 integer ) ;
SQL> create table two ( c1 integer , c2 integer ) ;
The following UNION ALL won't work:
SQL> select * from one union all select * from two ;
[mysqld-5.5.27]The used SELECT statements have a different number of columns
But this one is ok:
SQL> select * from one union all select *, null from two ;
Note we did add NULL for the missing (c3) column on table two. You can add whatever you want...
Since you not provided sample data, we can't suggest you correct way what and how to use, but I noticed that:
With JOIN you have incorrect syntax. Should be like:
SELECT *
FROM tbl1 t1
INNER JOIN tbl2 t2 ON t1.Id = t2.Id
If you want to use UNION - order of columns and number of columns should match in both tables, so you have to provide column names in both tables, in following:
SELECT Col1, Col2, Coln
FROM tbl1
UNION
SELECT Col1, Col2, Coln
FROM tbl2

Overall Unique count from two tables in MySQL

I have two tables, both having column a device_id column that I want to count. For the purposes of demonstration, the schema looks like:
Table 1: 'id', 'save_val', 'device_id_major'
Table 2: 'id', 'save_val', 'location', 'device_id_team'
Table 1 could have many of the same 'device_id_major'.
I basically want to get the unique device_id's from both tables, then from that result set, get the count of unique device_id's (the same device_id can appear in both tables).
Is this possible in one query?
select distinct aa.device_id, count(*)
from(select distinct device_id from table1
union all
select distinct device_id from table2) as aa
group by device_id
order by device_id
Or something like... As I don't have the schema to hand, I can't fully validate it.
SELECT count(DISTINCT aa.id)
FROM (SELECT DISTINCT major_id AS id FROM `major`
UNION ALL
SELECT DISTINCT team_id AS id FROM `team`)
AS aa
This seems to do the trick.
You could use a query that takes the UNION of both tables, then SELECT the unique values.