Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MySql select query with multiple LIKE clause not working

Posted on 2010-08-26
3
Medium Priority
?
349 Views
Last Modified: 2012-05-10
Hi,

I have the following MySql statement that is generated by a script based upon the input from the user.
If the user enters more than one term like in this case 'glass plates' I want it to locate all products where the product code or description is LIKE glass OR plates.
This is fine but slightly more complicated by the additional AND clauses which end up with the malformed query below. I have tried a few alternatives but this query result needs to be paginated and they did not seem to work.

SELECT ProdHead.ProdCode, ProdHead.ProdDesc, ProdSku.Colour  
FROM ProdHead
INNER JOIN ProdSku ON ProdHead.ProdCode = ProdSku.ProdCode
INNER JOIN ProdTitleIndex ON ProdHead.ProdCode = ProdTitleIndex.ProdCode
WHERE Concat(ProdHead.ProdDesc,Prodhead.prodcode) LIKE '%glass%'
OR Concat(ProdHead.ProdDesc,Prodhead.prodcode) LIKE '%plates%'
AND ProdHead.Avail = 1
AND ProdSku.Avail = 1
AND ProdHead.SubGroup > 0
AND ProdHead.SubGroup = 228
ORDER BY ProdHead.ProdDesc
LIMIT 0, 10

The only way I can get this to work is by repeating the AND clauses after each OR cluase which is just silly so I was hoping there may be a way of doing something such as

WHERE ........ IN('%glass%','%plates%')


Any help appreciated.

Regards
Leigh
0
Comment
Question by:Lmillard
[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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 33529809
>WHERE ........ IN('%glass%','%plates%')

sorry, won't work, unfortunately ...

apart from that: you mix AND an OR, so you need ()
SELECT ProdHead.ProdCode, ProdHead.ProdDesc, ProdSku.Colour  
FROM ProdHead
INNER JOIN ProdSku ON ProdHead.ProdCode = ProdSku.ProdCode
INNER JOIN ProdTitleIndex ON ProdHead.ProdCode = ProdTitleIndex.ProdCode
WHERE ( Concat(ProdHead.ProdDesc,Prodhead.prodcode) LIKE '%glass%'
     OR Concat(ProdHead.ProdDesc,Prodhead.prodcode) LIKE '%plates%'
      )
AND ProdHead.Avail = 1
AND ProdSku.Avail = 1
AND ProdHead.SubGroup > 0
AND ProdHead.SubGroup = 228
ORDER BY ProdHead.ProdDesc
LIMIT 0, 10 

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33529829
You need to properly bracket the LIKE's

SELECT ProdHead.ProdCode, ProdHead.ProdDesc, ProdSku.Colour  
FROM ProdHead
INNER JOIN ProdSku ON ProdHead.ProdCode = ProdSku.ProdCode
INNER JOIN ProdTitleIndex ON ProdHead.ProdCode = ProdTitleIndex.ProdCode
WHERE
(     Concat(ProdHead.ProdDesc,Prodhead.prodcode) LIKE '%glass%'
      OR Concat(ProdHead.ProdDesc,Prodhead.prodcode) LIKE '%plates%' )
AND ProdHead.Avail = 1
AND ProdSku.Avail = 1
AND ProdHead.SubGroup > 0
AND ProdHead.SubGroup = 228
ORDER BY ProdHead.ProdDesc
LIMIT 0, 10

The IN form doesn't work, IN only takes exact matches.
Though the query above will work, to perform decently, you may want to try splitting it out and timing each alternative.

SELECT ProdHead.ProdCode, ProdHead.ProdDesc, ProdSku.Colour  
FROM ProdHead
INNER JOIN ProdSku ON ProdHead.ProdCode = ProdSku.ProdCode
INNER JOIN ProdTitleIndex ON ProdHead.ProdCode = ProdTitleIndex.ProdCode
WHERE
(     ProdHead.ProdDesc LIKE '%glass%' OR
      Prodhead.prodcode LIKE '%glass%' OR
      ProdHead.ProdDesc LIKE '%plates%' OR
      Prodhead.prodcode LIKE '%plates%')
AND ProdHead.Avail = 1
AND ProdSku.Avail = 1
AND ProdHead.SubGroup > 0
AND ProdHead.SubGroup = 228
ORDER BY ProdHead.ProdDesc
LIMIT 0, 10
0
 

Author Closing Comment

by:Lmillard
ID: 33530049
Great, thanks very much
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

661 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