SQL Join multiple tables with the same column names - mysql

I'm guessing this is very simple but I'm totally new to this.
Could you please help to join these 3 or more tables with the same column name into one large table. Examples given below. I have table 2017 to 2019 and the resultant image at the bottom is what I am trying to get.
What is the SQL BigQuery code for this? Thanks!
--------WHAT I AM TRYING TO ACHIEVE ---------------

I'm pretty sure you want union all:
select t.*
from table_2017 t
union all
select t.*
from table_2018 t
union all
select t.*
from table_2019 t;
The use of t.* is a convenience, assuming the tables have the same names, in the same order with compatible types. I recommend listing the columns explicitly.
Note: You should probably fix your data model. Having multiple tables with the same columns is not a good data model. All this data should be in one table.

Related

mysql select all table data from two tables on two databases

I want to get all data from two table on different Databases.I've tried this and only get data from one table.
Select b1.branch.*, db2.branch.* from db1.branch,db2.branch
Is there any way to get that?
Assuming that your branch table have the same struct in both database then you could use an union eg:
Select db1.branch.*
from db1.branch
union all
select db2.branch.*
from db2.branch
for obtain both result sets

Subquery for fetching table name

I have a query like this :
SELECT * FROM (SELECT linktable FROM adm_linkedfields WHERE name = 'company') as cbo WHERE group='BEST'
Basically, the table name for the main query is fetched through the subquery.
I get an error that #1054 - Unknown column 'group' in 'where clause'
When I investigate (removing the where clause), I find that the query only returns the subquery result at all times.
Subquery table adm_linkedfields has structure id | name | linktable
Currently am using MySQL with PDO but the query should be compatible with major DBs (viz. Oracle, MSSQL, PgSQL and MySQL)
Update:
The subquery should return the name of the table for the main query. In this case it will return tbl_company
The table tbl_company for the main query has this structure :
id | name | group
Thanks in advance.
Dynamic SQL doesn't work like that, what you created is an inline-view, read up on that. What's more, you can't create a dynamic sql query that will work on every db. If you have a limited number of linktables you could try using left-joins or unions to select from all tables but if you don't have a good reason you don't want that.
Just select the tablename in one query and then make another one to access the right table (by creating the query string in php).
Here is an issue:
SELECT * FROM (SELECT linktable FROM adm_linkedfields WHERE name = 'company') as cbo
WHERE group='BEST';
You are selecting from DT which contains only one column "linktable", then you cant put any other column in where clause of outer block. Think in terms of blocks the outer select is refering a DT which contains only one column.
Your problem is similar when you try to do:
create table t1(x1 int);
select * from t1 where z1 = 7; //error
Your query is:
SELECT *
FROM (SELECT linktable
FROM adm_linkedfields
WHERE name = 'company'
) cbo
WHERE group='BEST'
First, if you are interested in cross-database compatibility, do not name columns or tables after SQL reserved words. group is a really, really bad name for a column.
Second, the from clause is returning a table containing a list of names (of tables, but that is irrelevant). There is no column called group, so that is the problem you are having.
What can you do to fix this? A naive solution would be to run the subquery, run it, and use the resulting table name in a dynamic statement to execute the query you want.
The fundamental problem is your data structure. Having multiple tables with the same structure is generally a sign of a bad design. You basically have two choices.
One. If you have control over the database structure, put all the data in a single table, linktable for instance. This would have the information for all companies, and a column for group (or whatever you rename it). This solution is compatible across all databases. If you have lots and lots of data in the tables (think tens of millions of rows), then you might think about partitioning the data for performance reasons.
Two. If you don't have control over the data, create a view that concatenates all the tables together. Something like:
create view vw_linktable as
select 'table1' as which, t.* from table1 t union all
select 'table2', t.* from table2 t
This is also compatible across all databases.

MySQL: Merge two tables (same schema) and create a new one - SINGLE QUERY

I have two tables with exactly the same schema.
I would like to have a 3rd table, containing all the data of these two tables combined.
How can I do this with an INSERT INTO query (single query doing this)?
I know I can do this with: INSERT INTOname_of_new_tableSELECT DISTINCT * FROMname_of_old_table but then I would need to do it twice. I am seeking to doing this with a SINGLE query.
Thank you.
INSERT INTO new_table
SELECT * FROM old_table_1
UNION
SELECT * FROM old_table_2
Removed your DISTINCT, as UNION has an implied DISTINCT (UNION ALL does not have this). This will only work if the tables are identical in column count and have similar column type, however. Then again, it sounds like that's your situation.

how can i query a table that got split to 2 smaller tables? Union? view?

I have a very big table (nearly 2,000,000 records) that got split to 2 smaller tables. one table contains only records from last week and the other contains all the rest (which is a lot...)
now i got some Stored Procedures / Functions that used to query the big table before it got split.
i still need them to query the union of both tables, however it seems that creating a View which uses the union statement between the two tables lasts forever...
that's my view:
CREATE VIEW `united_tables_view` AS select * from table1 union select * from table2;
and then i'd like to switch everywhere the Stored procedure select from 'oldBigTable' to select from 'united_tables_view'...
i've tried adding indexes to make the time shorter but nothing helps...
any Ideas?
PS
the view and union are my idea but any other creative idea would be perfect!
bring it on!
thanks!
If there is a reason not to, you should merge the tables rather than constantly query both of them.
Here is question on StackOverflow about doing that:
How can I merge two MySQL tables?
If you need to keep them seperate, you can use syntax along the lines of:
SELECT table1.column1, table2.column2 FROM table1, table2 WHERE table1.column1 = table2.column1;
Here is an article about when to use SELECT, JOIN and UNION
https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-1050307.html

how to select all the data from many tables?

how to select all the data from many tables?
i try
`"SELECT * FROM `table1`, `table2`"`
,
but result none understandable for me. it returns only some rows from table1, and 3 times all the data from table2. i've red one same question here, but don't understand the answer. so could you help me? thanks in advance.
update:
when i try
(SELECT * FROM `table1`) UNION (SELECT * FROM `table2`)
it returns
#1222 - The used SELECT statements have a different number of columns
By doing that select with the "," between 2 tables and no WHERE clause, you are doing an implicit cross join of the 2 tables (all combinations of rows between the 2 tables). This is likely Not What You Want. See UNION, as mentioned by other answers.
Use the UNION SELECT construct
How do you want the data displayed? Are both tables of the same schema? If so you could use the UNION operator.
http://www.w3schools.com/sql/sql_union.asp
If you are just trying to show the data from many tables and there is no relationship between the data, you have to program logic instead of database logic.
show tables (SQL command)
foreach result (programming language of your choice)
select * from tablename (SQL command)