Multiple SQL SELECT
I've 10 tables with a lot of records. All tables have "Date" column. I want extract all data from tables for date. I can do 10 queries SELECT * FROM Table1 开发者_如何学CWHERE Date=dd/MM/yyyy, ect...but I want to do only a query with "multiple selection". How can I do this? I'm not so skilled with SQL language.
EDIT: I'm working with Microsoft Access and also MySQL (for two different desktop application, but same problem). Tables have different fields (just Date all in common), so It's not good the use of UNION.
SELECT *
FROM table1,
table2
WHERE table1.date = 'somedate'
AND table2.date = 'somedate'
Take a look at the UNION operator for including data from multiple SELECT statements.
Your question is not so clear. Based one what i understood, you can use Union SQL statement to combine your queries and make them as a single query. But if you want to query for different dates in different tables, you can use only use multiple queries .
If I right understand your question, and you want to get data from tables where all 10 tables contains the same list of fields, you can use
SELECT * FROM Table1 WHERE Date=dd/MM/yyyy
UNION ALL
SELECT * FROM Table2 WHERE Date=dd/MM/yyyy
UNION ALL
...
UNION ALL
SELECT * FROM Table10 WHERE Date=dd/MM/yyyy
UNION ALL
If fields are different, you need to add the fields you want to get in result set:
SELECT field1, field2 FROM Table1 WHERE Date=dd/MM/yyyy
UNION ALL
SELECT field1, field2 FROM Table2 WHERE Date=dd/MM/yyyy
UNION ALL
...
UNION ALL
SELECT field1, field2 FROM Table10 WHERE Date=dd/MM/yyyy
UNION ALL
Be careful of performance issues when using Union. -- Be sure to run some tests comparing query times of the two approaches.
Depending on your database software, you could also use a stored procedure.
And also check that the date column is indexed in each of the tables.
精彩评论