Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 5th, 2004, 01:44 PM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default %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
Reply With Quote
  #2 (permalink)  
Old 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
Default

You might want to try putting parentheses around the LIKE statements.

Reply With Quote
  #3 (permalink)  
Old 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
Default

Hi David,
Quote:
quote:avail = 'yes'
What datatype is AVAIL there in the table?

Cheers!

_________________________
- Vijay G
Strive for Perfection
Reply With Quote
  #4 (permalink)  
Old November 8th, 2004, 01:53 PM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #5 (permalink)  
Old 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
Default

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
Reply With Quote
  #6 (permalink)  
Old November 9th, 2004, 07:06 PM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
how to make a query from an existing query raport SQL Language 3 November 13th, 2006 08:59 PM
I solved insert query.now see this Update Query. amit_mande@yahoo.com VB.NET 2002/2003 Basics 2 September 21st, 2006 12:48 AM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM
Error on Make-Table Query In Union Query rylemer Access 1 August 20th, 2003 07:42 PM



All times are GMT -4. The time now is 02:24 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.