Solved

2 tables same layout want to use only one where clause

Posted on 2014-04-16
3
192 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 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