?
Solved

Multiple Queries but no join

Posted on 2013-06-18
3
Medium Priority
?
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 71

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 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 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 49

Accepted Solution

by:
PortletPaul earned 1000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 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