I have multiple tables named as: data_yyyymmdd.
Ex. data_20121028, data_20121029, ...
Each table have same fields. How can I get all the records from all those tables?
SELECT * FROM data_20121028
UNION
SELECT * FROM data_20121029
UNION
...
MySQL 5.6 Reference Manual: UNION Syntax
Related
I'm trying to retrieve all tables with specific name format,
for performing union among those tables.
I'm using mysql Ver 8.0.13, and i wrote this following query for retrieving the relevant tables:
show tables LIKE REGEX '^table_.+_class$';
I couldn't figure out the correct syntax for this query :/
Afterwards i'm planning to union all those tables.
I would like to avoid writing this code since it doesn't scale nicely:
SELECT * FROM table_french_class
UNION
SELECT * FROM table_history_class
UNION
SELECT * FROM table_pingpong_class
UNION
SELECT * FROM table_math_class
UNION
SELECT * FROM table_literature_class
Can someone suggest me how to handle this issue?
Thank you
You could use INFORMATION_SCHEMA catalog:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME REGEXP '^table_.+_class$';
I have 2 databases. In database1, I want to run a query from database2 and UNION ALL the results. This is the syntax I tried, but I get an error of
Syntax error in from clause
Here is the syntax I tried --- where is my error?
SELECT * FROM query1
UNION ALL
SELECT * FROM query1 IN C:\Database\production.mdb
Add quotes around the path of the external database.
I would also use aliases to distinguish between the 2 instances of query1.
SELECT q1Local.* FROM query1 AS q1Local
UNION ALL
SELECT q1Remote.* FROM query1 AS q1Remote IN 'C:\Database\production.mdb'
Is it possible to join Two queries of mysql in a query ??
Like:
select * from a + select * from b
So that I can use them in a single php loop.
If they have the same number of columns and the datatypes are the same in each column, then you can use a UNION or UNION ALL:
select *
from a
UNION ALL
select *
from b
If you provide more details about the tables, data, etc, then there might be another way of returning this data.
A UNION will return only the DISTINCT values, while a UNION ALL selects all values.
If this is the route that you need to take, and you still need to identify which table the data came from, then you can always create a column to identify which table the data is from , similar to this:
select *, 'a' TableName
from a
UNION ALL
select *, 'b' TableName
from b
This allows you to distinguish what table the data came from.
I think it is easier creating sql "variables" like:
select varA, varb from TableA, tableB;
and you can just play with values in PHP accessing properties.
That way you can take conditions in the query like:
select varA, varb from TableA, tableB where varA.id = varB.foreingId bla bla...
;)
Is it possible in MySQL to select from a static set of integers? Given an array of integers like 1 and 2, what is the correct syntax in MySQL to do something like:
select
*
from
(values(1),(2))
Which should return one row containing the number 1 and one row containing the number 2.
In other SQL than MySQL (e.g. MSSQL) this is possible. Is there a way to do this in MySQL?
I think you mean something like this?
SELECT 1 UNION SELECT 2 UNION SELECT 3
The only way to create a virtual set in MySQL is using a subquery with UNION. The subquery in the FROM clause creates 3 rows which can be joined with another table:
SELECT foo.*
FROM (
SELECT 1 AS num UNION
SELECT 2 AS num UNION
SELECT 3 AS num
) virtual
LEFT JOIN foo ON foo.num = virtual.num
When you want to use your list of values as a condition for WHERE or ON clause, then the IN() construct may be the right way to go:
SELECT foo.* FROM foo WHERE foo.num IN (1,2,3)
sorry for my english
you can use (IN) like this
SELECT * FROM Table WHERE id IN (1,2,3....)
I have a two tables A and B
I would like to union them and store in to another table;
CREATE TABLE myspace.test (
(select * from A ) UNION ( select * from B) );
It fails with an 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 'CREATE TABLE
myspace.test ( (select * from A )
UNION ( s' at line 1
But the query with: (select * from A ) UNION ( select * from B) gives correct result.
How to store union result in to another table??
Thanks Arman.
EDIT
Well After playing around I found that:
The query without outer brackets works.
CREATE TABLE myspace.test
(select * from A ) UNION ( select * from B) ;
Adding AS is not solving the problem.
I was wondered that query with brackets is working well seems tome BUG or maybe I am missing something?
CREATE TABLE myspace.test
(select * from A);
CREATE TABLE
myspace.test
AS
SELECT *
FROM A
UNION
SELECT *
FROM B
Read the documentation. There are no parens around the select-statement.
CREATE TABLE `myspace`.`test` (SELECT * FROM `A`) UNION (SELECT * FROM `B`);
Watch out for duplicate primary keys, though. You may want to consider first creating an empty table myspace.test with the proper layout, then inserting rows into it more selectively.
This does not look right at first sight.
Start by simply creating your new empty table using CREATE TABLE.
Then run a query to populate it, should be something like
INSERT INTO newTable(field1, field2,..., fieldN)
SELECT temp.field1, temp.field2,...,temp.fieldN
FROM
(
SELECT field1, field2,...,fieldN
FROM A
UNION
SELECT field1, field2,...,fieldN
FROM B
) temp
Hope this helps!