Solved

2 tables same layout want to use only one where clause

Posted on 2014-04-16
3
189 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
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:Kdo
Kdo 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP get months from date search query where user 40 61
Filter by data and current_date 4 39
MySQL - Limit or Top Records 15 43
phpMyAdmin simple sql statement 3 52
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Creating and Managing Databases with phpMyAdmin in cPanel.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

16 Experts available now in Live!

Get 1:1 Help Now