?
Solved

Multiple Queries but no join

Posted on 2013-06-18
3
Medium Priority
?
267 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 70

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 70

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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

777 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