Solved

2 tables same layout want to use only one where clause

Posted on 2014-04-16
3
194 Views
Last Modified: 2014-04-16
select * from a_messages2 where profile_id like 'kb%'
union
select * from a_messages where profile_id like 'kb%'
order by message_text

same table layout
order by message_text applys to both tables

I tried putting where profile_id like 'kb%'  and got error
0
Comment
Question by:rgb192
[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
3 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 400 total points
ID: 40003579
Try this:
SELECT * FROM (
SELECT * FROM a_messages2
UNION
SELECT * FROM a_messages
) t WHERE profile_id LIKE 'kb%'
ORDER BY message_text

Open in new window

HTH,
Dan
0
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 100 total points
ID: 40003741
Dan's approach will work, but as the tables get bigger the performance will quickly decline.  The UNION negates the ability to use the table index(es) so you'll essentially have multiple full table scans.

You should probably stick with using a where clause on each sub-select.  If you really need to use just a single where clause (and I can't imagine any real need for it) at least use UNION ALL over UNION.


Good Luck,
Kent
0
 

Author Closing Comment

by:rgb192
ID: 40003895
thanks for query and thanks for performance advice
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

630 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