473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

LIKE and IN can I combine

I can use the IN with the WHERE clause as example:

SELECT * FROM NORTHWIND WHERE LASTNAME IN ('FULLER','KING')

I want to use the IN and LIKE at the same time:
SELECT * FROM NORTHWIND WHERE LASTNAME LIKE ('A%','B%')

I know this is a simplistic example, but the members for the IN will be
many, 5 to 10.
I'm trying to avoid:
SELECT * FROM NORTHWIND WHERE LASTNAME LIKE 'A%' OR LASTNAME LIKE 'B%'
OR LASTNAME LIKE 'FU%' OR LASTNAME LIKE 'JON%' <...>

and so forth.
Any Ideas?
TIA
Rob

Oct 10 '05 #1
3 14589
Hello, Rob

Instead of this:

SELECT * FROM employees
WHERE LastName LIKE 'C%'
OR LastName LIKE 'D%'
OR LastName LIKE 'F%'

you can use:

SELECT e.* FROM employees e INNER JOIN (
SELECT 'C' AS Prefix
UNION ALL SELECT 'D'
UNION ALL SELECT 'F'
) x ON e.LastName LIKE x.Prefix+'%'

or:

SELECT * FROM employees e
WHERE EXISTS (
SELECT * FROM (
SELECT 'C' AS Prefix
UNION ALL SELECT 'D'
UNION ALL SELECT 'F'
) x WHERE e.LastName LIKE x.Prefix+'%'
)

The last query is more correct than the previous (in case that two
prefixes could match the same name).

Of course, it's better to use a temporary table to store all the
requested prefixes.

Razvan

Oct 10 '05 #2
On 10 Oct 2005 10:19:34 -0700, Razvan Socol wrote:
SELECT * FROM employees e
WHERE EXISTS (
SELECT * FROM (
SELECT 'C' AS Prefix
UNION ALL SELECT 'D'
UNION ALL SELECT 'F'
) x WHERE e.LastName LIKE x.Prefix+'%'
)


Is there any good reason why you wouldn't do this variation:

SELECT * FROM employees e
WHERE EXISTS (
SELECT * FROM (
SELECT 'C%' AS Pattern
UNION ALL SELECT 'D%'
UNION ALL SELECT 'F%'
) x WHERE e.LastName LIKE x.Pattern
)
Oct 10 '05 #3
That would be OK, too.

Razvan

Oct 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tom | last post by:
I have two seperate pages, one is my navigation and the other is my main page. How do I combine the two useing php, if you know please gimme a hand. thanks
3
by: mmccaws | last post by:
Thanks ahead for your help I'm trying to learn what I can do with echo and print statements. I figured out the echo statement and below is the simple version using print. I've tried two dozen...
4
by: MichaelD | last post by:
Hi! How do I combine an element pattern with attributes? I can validate <phone>111-111-1111</phone> using pattern
2
by: tina.boroff | last post by:
I didn't know what to use as a title for this. Here is the scenario: I have a op.reqedit.php page. This page a form where you input data. In this page I have split a MySQL field. It is called...
15
by: Lee | last post by:
Often times, I do not want to specify the width of a div. Rather, I just want it to expand to be just wide enough to enclose its contents. Since this is how tables behave, I tried setting "div...
2
by: Knighterrant | last post by:
How to combine multiple xml files into a single one, or split an xml file into multiple files using xslt?
1
by: William Stacey [MVP] | last post by:
I need a bullet proof way to combine a root and a relative path to form a FQ rooted path (similar to a VDir in IIS). Path.Combine alone will not do the job in all cases. I also need to be sure...
1
by: David Lozzi | last post by:
Hello, I'm looking for the best option to combine two XMLDocuments into a single document. I'm using ASP.Net using VB. I have a function that returns a dataset in XML which works great if there...
3
by: bearophileHUGS | last post by:
Psyco is finished now, and it works on the x86, for Win, the new macs, many linux boxes, etc, and it's quite useful, so maybe it can be added to the standard Python distribution. PyChecker (and...
1
by: bluereign | last post by:
Thank you for your assistance. I am a novice looking to JOIN and append or combine records from 2 current Tables into 2 new Tables named below. I have been able to JOIN Tables with the script...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.