4 columns from different tables in 1 query

adilling

New member
Local time
Yesterday, 23:00
Joined
Jun 27, 2002
Messages
8
I have 4 tables that have 2 columns, plus the primary ID column. The 2 columns are Type and Date. In my query I'd like to combine all 4 of these tables' column's in simply 2 columns of type and date. how do i do this?
 
union query

i just read something about a union query using SQL? is this what i need? and if so how do i write that in the sql sheet?
 
Ok, I used this code to get partially what i wanted:

SELECT [Type], [Date]
FROM [Driver's License]
Where [Date]<DATE()+30
UNION SELECT[Type], [Date]
FROM [Driving Record]
Where [Date]<DATE()+30
UNION SELECT [Type], [Date]
FROM [Medical Record]
Where [Date]<DATE()+30
UNION SELECT[Type], [Date]
FROM [Violations Record]
Where [Date]<DATE()+30;

BUT, how do I get 2 columns from another table (Name and Plant) that are related by the primary key?
 
I just looked it up and this is what my book says:

SELECT [data], [type]
FROM [NameofTable]

UNION SELECT [data], [type]
FROM [Name of second table]

etc....

Each SELECT statement requires the same number of fields, in the same order.

When you use Union command in the SQL SELECT statement, it only copies records that are NOT duplicates when it joins that tables. If you want to copy ALL records simply use the keyword ALL after the UNION command: i.e., UNION ALL SELECT.

I hope this will help you.
Marina
 
thanks, but how do I get new columns using my primary key? I can do it fine with a normal query but how do with union query?
 
You would have less trouble if you constructed the db correctly. You do not need 4 tables just one will do. In very simple terms a date field which should not be named date, and a field which reflects the type. Use a combo box to select one of four available options which is what you seem to have at present.
 

Users who are viewing this thread

Back
Top Bottom