|
 |
MySQL General discussion about the MySQL database. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the MySQL section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
|
 |
|
|
 |

November 5th, 2004, 01:44 PM
|
Authorized User
|
|
Join Date: Oct 2003
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
%like% query
Hi,
I am trying to use a MySQL query to show results using a %like% statement and also specify one field is equal to 'yes', here is my query:
--------
SELECT * FROM item WHERE itemDesc like '%searchterm%' or itemName like '%searchterm%'
--------
This works fine except I want to only show item where avail(ability) is equal to yes, this works:
--------
SELECT *
FROM item
WHERE itemDesc like '%varsearch%' AND avail = 'yes'
---------
but when I try and search both columns it just does the query and ignores the 'avail=yes'?
This is essentially what I want to achieve:
-------------
SELECT *
FROM item
WHERE itemDesc like '%searchterm%' or itemName like '%searchterm%' AND avail = 'yes'
-------------
I have tried lot of ways but think I am approaching this wrong, please can you help,
Many thanks
David
|

November 5th, 2004, 02:28 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
You might want to try putting parentheses around the LIKE statements.
|

November 8th, 2004, 11:59 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi David, What datatype is AVAIL there in the table?
Cheers!
_________________________
- Vijay G
Strive for Perfection
|

November 8th, 2004, 01:53 PM
|
Authorized User
|
|
Join Date: Oct 2003
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Vijay,
I managed to do it with advice from Stu like this:
SELECT *
FROM item
WHERE (itemDesc like '%varsearch%') AND avail = 'yes' OR itemName like '%varsearch%' AND avail='yes'
I don't know if this is correct but I now get the desired results from it.
To answer your question:
The avail column is enum('yes','under offer','no')
Any input very welcome
Thanks
David
|

November 9th, 2004, 05:49 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi David,
You could still simplify that by using this.
Code:
WHERE (itemDesc like '%varsearch%' OR itemName like '%varsearch%') AND avail='yes'
Hope that helps.
Cheers!
_________________________
- Vijay G
Strive for Perfection
|

November 9th, 2004, 07:06 PM
|
Authorized User
|
|
Join Date: Oct 2003
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Vijay,
Thanks for the help, the code works great now, and with your help nice and simplified.
I have it working here http://www.hiltonmedals.co.uk/search.php
Thanks
David
|
Thread Tools |
Search this Thread |
|
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |