Solved

Multiple Queries but no join

Posted on 2013-06-18
3
257 Views
Last Modified: 2013-07-05
I have 5 tables, all unrelated to each other, however I want to perform a search on them.

Each table are different in their construction, with 3 constants, a PRI-ID which is an auto-number, only used for updating and deleting, a date field and a `Notes` field

Currently I have 5 queries:-
SELECT *, `1` as `Table` FROM `Table1` WHERE `Notes` LIKE '%name%' ORDER BY `crDate`;
SELECT *, `2` as `Table` FROM `Table2` WHERE `Notes` LIKE '%name%' ORDER BY `crDate`;
SELECT *, `3` as `Table` FROM `Table3` WHERE `Notes` LIKE '%name%' ORDER BY `crDate`;
SELECT *, `4` as `Table` FROM `Table4` WHERE `Notes` LIKE '%name%' ORDER BY `crDate`;
SELECT *, `5` as `Table` FROM `Table5` WHERE `Notes` LIKE '%name%' ORDER BY `crDate`;

Open in new window


Currently I execute each one and make the table up as required, however I would ideally like to sort by created date, so I managed to join all the queries:-
SELECT `Table1`.`*`, `Table2`.`*`, `Table3`.`*`, `Table4`.`*`, `Table5`.`*`
FROM `Table1`, `Table2`, `Table3`, `Table4`, `Table5`

Open in new window


Which works great, however sorting by crDate in each table as simply "SORT BY `crDate` ASC" doesn't work, and if I sort by date on individual tables, then it correctly sorts Table1, then adds onto a correctly sorted Table2 etc, it doesnt combine the 2 tables and sort by that.

I'm hoping that by merging crDate into 1 field and then sorting, will also help me search the notes field.

Any ideas?

Thank you
0
Comment
Question by:tonelm54
  • 2
3 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 39257137
How about using UNION ALL?
SELECT *, `1` as `Table` FROM `Table1` WHERE `Notes` LIKE '%name%'
UNION ALL
SELECT *, `2` as `Table` FROM `Table2` WHERE `Notes` LIKE '%name%'
UNION ALL
SELECT *, `3` as `Table` FROM `Table3` WHERE `Notes` LIKE '%name%'
UNION ALL
SELECT *, `4` as `Table` FROM `Table4` WHERE `Notes` LIKE '%name%'
UNION ALL
SELECT *, `5` as `Table` FROM `Table5` WHERE `Notes` LIKE '%name%'
ORDER BY `crDate`;

Open in new window

0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 39257141
Don't know if MySQL supports it, but it would be much more convinient to have:
select * from
(
SELECT *, `1` as `Table` FROM `Table1` 
UNION ALL
SELECT *, `2` as `Table` FROM `Table2`
UNION ALL
SELECT *, `3` as `Table` FROM `Table3`
UNION ALL
SELECT *, `4` as `Table` FROM `Table4`
UNION ALL
SELECT *, `5` as `Table` FROM `Table5`
) data
WHERE `Notes` LIKE '%name%'
ORDER BY `crDate`;

Open in new window

0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 total points
ID: 39258363
A word of caution. UNION ALL is quite appropriate BUT, the field list MUST be the same AND every column MUST be the same data type. So in the above comments just be aware that:

select *

cannot be taken literally, you MUST specify the fields, and they must be the same data types
select
    `frmTable`
  , `crDate`
  , `notes`
from
    (
        SELECT '1' as `frmTable` , `crdate`, `notes` /* more ?? */
        FROM `Table1` 
        UNION ALL
        SELECT '2' as `frmTable` , `crdate`, `notes` /* more ?? */
        FROM `Table2`
        UNION ALL
        SELECT '3' as `frmTable` , `crdate`, `notes` /* more ?? */
        FROM `Table3`
        UNION ALL
        SELECT '4' as `frmTable` , `crdate`, `notes` /* more ?? */
        FROM `Table4`
        UNION ALL
        SELECT '5' as `frmTable` , `crdate`, `notes` /* more ?? */
        FROM `Table5`
    ) data
WHERE `Notes` LIKE '%name%'
ORDER BY `crDate`;

Open in new window

MySQL does support nested subqueries - but it can be slower - for more recent versions this shouldn't be an issue I think.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now