Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

2 tables same layout want to use only one where clause

Posted on 2014-04-16
3
Medium Priority
?
195 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 1600 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 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 400 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In this article, we’ll look at how to deploy ProxySQL.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
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 Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

715 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