0

I'm doing a price table, and I was wondering if there is a way to match multiple columns with different values base on the values from a form... please allow me.

Table: Price
id | name | color | class | year | price
________________________________________
1  | BMW  | Red   | XML   | 2001 | 1200
2  | BMW  | Red   | XML   | 2003 | 1201
3  | BMW  | Blue  | LXX   | 2004 | 1230
4  | VW   | Red   | LXS   | 2001 | 1100
5  | VW   | Blue  | LXV   | 2003 | 1103

Basically my table looks like that, the idea is to get the price value by matching four columns... if name is bmw and color is red and class is xml and year is 2001 then the price is 1200

How do I make that query?... I don't have any code for this type of query just yet... but I was thinking to do a few queries base on the first value....

SELECT * FROM price 
WHERE name = '$user_query' AND (color = 'Red' OR class = 'XML' )
if [...] ... that just wont work...

I don't know how to do that query... I can go one by one matching the inputs from the form but I will have to make many queries... four queries actually... I'd love to simplified my query to get that value from column price base on the request from the user...

The other thing is to use switch case : value from the first query ... or use if, elseif .... r just IF ... that is because I know the values that the user will chose from the form and the form has only 4 selects ... one for each "column" name, color, class and year

You may think that I want you to do this for me... well yes and no... yes because I want to learn how to do this type of queries and NO because I have this combinations using IF's without a data base... I'd like to use a data base...

if ($name == 'bmw' && $color == 'red' && [...]) {
     $price = 1200;
} [...] and so on...

about 23 elseif {} ...

Thank you for taking the time and share your thoughts on this idea...


** E D I T **

So far I have two good answers and I'm using both, and for that I have a bit of trouble choosing the right answer...

Mahmoud Gamal and Rajesh Paul

So I'm going to leave it like that for a few more days whichever gets more votes if any, thats the one that I'm going to choose...

Thank you.

3 Answers 3

0

if it's about reducing the query length then I suggest this-

SELECT * FROM price 
WHERE (name, color, class) = ('$user_query', 'Red', 'XML' );

Just grouping the set of attributes instead of cascading the conditions using AND, OR definitely reduces the length of the query.

4
  • @Tanker Didn't it go well? Dec 22, 2013 at 11:22
  • Sorry it toke me some time to get back, I fall asleep... and yes, your technique worked just fine, as well Mahmoud Gamal's technique, funny thing is that I have two queries with different purpose in the same file, in one I'm using your technique and in the other I'm using Mahmoud's technique... one question though, which one do you prefer to use, $_GET or $_POST... Thank you...
    – Tanker
    Dec 22, 2013 at 13:02
  • Usually $_POST for form submission. Dec 22, 2013 at 13:49
  • @Tanker As I understood I thought you needed a simple way to write up the query. But now it feels that the cascading clause i.e. either AND or OR may vary. My implementation is based on the use of AND. Am I correct about what you need? Dec 22, 2013 at 13:58
0

You must change the syntaxis

WHERE name = '$user_query' AND (color = 'Red' OR class = 'XML' )

WHERE name = '$user_query' AND color = 'Red' AND class = 'XML'
0

What you need here is a optional parameters in the WHERE clause:

SELECT * 
FROM price 
WHERE ($ColorPatam IS NULL OR Color = $ColorParam)
  AND ($ClassParam IS NULL OR Class = $ClassParam)
  ...

If one of the parameters has a NULL values, then the expression is ignored.

2
  • Hello, so, what you are telling me is that I can use as many AND as I need? I'm asking because before I decide to change my IF's to an SQL query, I had another table with almost similar situation but in that case it was a single column with two main values.. so the query didn't allow me to use more than one AND... SELECT a FROM t WHERE b = '1' AND (c='1' or c='2') ORDER by d ASC something like that ... but if I can use multiple AND's .. then I guess I'm good to go... I'll do the table right now and give it a try... thank you.
    – Tanker
    Dec 22, 2013 at 9:06
  • @Tanker - For the other table you can use WHERE (@columname IS NULL OR (columnname = @columnname)) the same way. You just need to check the input in your form, if it has no value then set the parameter @foo with NULL and you are done. Dec 22, 2013 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.