SQL Query Help

  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

Hello All,
Please give me a hand here, I need this query fixed now. This is what it looks like at the moment:


Code: [ Select ]
SELECT `key` , `user` , `name` , `description` , `price`
FROM `p`
WHERE 1 AND `name`
LIKE '%test%' AND `price` < 120 LIMIT 0 , 30
  1. SELECT `key` , `user` , `name` , `description` , `price`
  2. FROM `p`
  3. WHERE 1 AND `name`
  4. LIKE '%test%' AND `price` < 120 LIMIT 0 , 30


but I need it to do more than this.

1. I need to display all records that contain the text "test" somewhere in the key, name, or description fields
2. I need to limit the price to between two numbers (greater than 80 and less than 120)

Thanks in advance.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Lieven
  • Beginner
  • Beginner
  • User avatar
  • Posts: 57
  • Loc: Belgium

Post 3+ Months Ago

Code: [ Select ]
SELECT key,user,name,description,price
FROM p
WHERE (name LIKE '%test%'
OR key LIKE '%test%'
OR description LIKE '%test%')
AND price>=80
AND price<=120
  1. SELECT key,user,name,description,price
  2. FROM p
  3. WHERE (name LIKE '%test%'
  4. OR key LIKE '%test%'
  5. OR description LIKE '%test%')
  6. AND price>=80
  7. AND price<=120
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

THANK YOU so much for your fast reply - now i have solved my problem and i am all the wiser on SQL syntax - All my participation points go to you, my good friend.
  • Lieven
  • Beginner
  • Beginner
  • User avatar
  • Posts: 57
  • Loc: Belgium

Post 3+ Months Ago

No problemo and thanks for the points...

If you want a small tutorial, you should have a read at http://www.mysql.com/doc/en/Retrieving_data.html
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

could I please have a hand on this problem as well? When I queried the database, nothing was returned to the script. I retreived the error, and it says:
fetch() without execute()

I don't have a clue what that means. Could you please give me a hand?


By the way, that's a great tutorial - it's a lot nicer than using phpmyadmin to generate queries and then subbing in the variables with perl.
  • Lieven
  • Beginner
  • Beginner
  • User avatar
  • Posts: 57
  • Loc: Belgium

Post 3+ Months Ago

It's because naming a column 'key' isn't a good idea since it's a reserved keyword in SQL (it's used for other stuff)

you should put `key` with the ` things to let your SQL server know you want the column named key instead of the keyword key.

SELECT `key`,name,description,price
FROM p
WHERE (name LIKE '%test%'
OR `key` LIKE '%test%'
OR description LIKE '%test%')
AND price>=80
AND price<=120

Though this ` thing works, I would recommend to just rename the column...

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 118 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.