1

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 WHERE 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.

3
  • 2
    Can you be more specific? Table structures? DBMS? Commented Aug 17, 2011 at 0:33
  • What EXACTLY are you trying to select? We need more info. You have Table1 and Table2, ..., each with a Date column, and you want to select data from them where Date matches something? But how are these Tables related? Commented Aug 17, 2011 at 0:56
  • If you won't use UNION then put the value in a table then join your ten tables to that table. But why not use UNION? Commented Aug 17, 2011 at 7:45

5 Answers 5

2
SELECT *
FROM table1,
     table2
WHERE table1.date = 'somedate'
  AND table2.date = 'somedate'
Sign up to request clarification or add additional context in comments.

2 Comments

Umm will this work if table2 does not have any records with somedate but table1 does?
Then remove the 'AND table2.date = 'somedate'
0

Take a look at the UNION operator for including data from multiple SELECT statements.

Comments

0

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 .

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.