Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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
 
Old November 5th, 2004, 02:28 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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

 
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
 
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
 
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
 
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




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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.