How could I reduce this query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

How could I reduce this query

Post by impulse() »

Code: Select all

SELECT DISTINCT product
FROM scheme.opservm
WHERE product LIKE 'A%'
OR product LIKE 'B%'
OR product LIKE 'C%'
OR product LIKE 'D%'
OR product LIKE 'E%'
Regards,
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

It's less code, and it works, but I'm not sure it's really a good idea:

Code: Select all

SELECT DISTINCT `product` FROM `scheme.opservm` WHERE LEFT(`product`,1) IN ('A','B','C','D','E');
I'd definitely try benchmarking it because it might well be a lot slower.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Is there a tool for MySQL benchmarking or is just the case of comparing the "Query took x" time for each query?
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Also,

Is it possible to use the BETWEEN syntax on letters.

E.G:

Code: Select all

SELECT DISTINCT product
FROM scheme.opservm
WHERE LEFT (product, 1) BETWEEN ('A' AND 'Z')
I realise that query doesn't work but I'm just trying to make my question clearer.

Regards,
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Yes, you can do that. Remove the brackets after BETWEEN.
Post Reply