Solved

2 tables same layout want to use only one where clause

Posted on 2014-04-16
3
188 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
3 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 400 total points
Comment Utility
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:Kdo
Kdo earned 100 total points
Comment Utility
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
Comment Utility
thanks for query and thanks for performance advice
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now