Jump to content

Recommended Posts

I got several tables that are identical as structure. I need to select all the records from those tables,

filtered and ordered by date column .

 

So far no problem :

 

(SELECT * FROM table1 WHERE (Category Like '%Category')) UNION

(SELECT * FROM table2 WHERE (Category Like '%Category')) UNION

(SELECT * FROM table3 WHERE (Category Like '%Category')) UNION

(SELECT * FROM table4 WHERE (Category Like '%Category')) UNION

(SELECT * FROM table5 WHERE (Category Like '%Category')) ORDER BY Data

 

I want my records to be sorted by date as you see in the querry above ... but ... if there are several records

from table1 and table5 from the same day, it seems that MYSQL will return first the table1 records and after that

table5 records, even if some table5 records are newer.

 

for example

table1

1  aaa  2008-01-22

2  bbb 2008-01-21

 

table5

1  ccc  2008-01-27

2  ddd  2008-01-10

 

The correct result should be

2  ddd  2008-01-10

2  bbb 2008-01-21

1  aaa  2008-01-22

1  ccc  2008-01-27

 

The result I get

2  bbb 2008-01-21

1  aaa  2008-01-22

2  ddd  2008-01-10

1  ccc  2008-01-27

 

I tried the querry in SQL SERVER and I get the correct result every time

 

Where is my mistake ? or what can I change to get the correct result without puting the querry into a view or

using just one  table containing all the records

Link to comment
https://forums.phpfreaks.com/topic/88529-select-from-several-identical-tables/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.