1

I got stuck with a simple query which I can't figure out why isn't doing what I expect it to do. I have 3 values set on database like this:

$measure = 'kg';
$country_code = 'DE';
$weight = '5';

WEIGHT_UNIT | COUNTRIES | MAX_WEIGHT | PRICE
kg          | DE,AT     | 10         | 25.55
lbs         | DE,AT,CH  | 5          | 15.99 

My PHP query looks like this:

SELECT *
FROM `article_shipping_options`
WHERE `weight_unit` = '$measure'
    AND `countries` LIKE '%$country_code%'
    AND `max_weight` <= '$weight'
LIMIT 1;

The result I was expecting was the row with the 25.55 price.

I know I am doing something wrong here despise my 2 days search on google...any help would be mostly appreciated :)

7
  • var_dump(10 <= 5); // return false Commented Mar 17, 2013 at 13:02
  • then what is result of query? Commented Mar 17, 2013 at 13:11
  • Don't store comma separated values in a single database column if you're going to use it for querying. Commented Mar 17, 2013 at 13:12
  • Could you do a DESCRIBE table_name and paste the results? Commented Mar 17, 2013 at 13:13
  • @ypercube, I did read wiki from QUERY atg and it says, A request to retrieve information from a database or other information system. This tag can be used to refer to SQL queries against a database (SQL-Server, Oracle, MySQL, etc.) so I added it here. The last question was a mistake. Commented Mar 17, 2013 at 13:13

4 Answers 4

1

Did you mean MAX_WEIGHT >= $weight ?

Sign up to request clarification or add additional context in comments.

2 Comments

but if the operator WAS the issue, it would be a solution.
SOLUTION: then you might have defined the max_weight field as a numerical field in which case you should remove the quotation marks around the $weight variable? – by mavili under matt post.
1

I think you have the wrong inequality operator. Shouldn't it be max_weight >= '$weight'?

1 Comment

Thanks for your reply! Yes, I've tried that too with no success. This thing is just driving me crazy :D I used operators alot in the past but this is the first time I got stuck like this.
0

Try using FIND_IN_SET() and use max_weight >= '$weight'

SELECT * 
FROM   article_shipping_options 
WHERE  weight_unit='$measure' AND 
       FIND_IN_SET($country_code, countries) > 0  AND 
       max_weight >= '$weight' 
LIMIT  1;

1 Comment

Thanks for the quick reply, I appreciate it! Unfortunatelly FIND_INT_SET does the same thing as my query is doing. I think the problem lies on the operator part.
0

You have $weight set to 5, but in the row's MAX_HEIGHT is 10.

Then the last condition for that row evaluates as 10 <= 5. Since the condition was not met, the row was not returned.

7 Comments

Yes, I've tries that too...no success so far :(
then you might have defined the max_weight field as a numerical field in which case you should remove the quotation marks around the $weight variable?
BINGO! Thank you so much! I can't believe I did such stupid error. I've paid half of my sunday for it but thanks to you I can rest a bit now!
@everybody: Thank you all for trying to help, I really appreciate it!
@Jay so what was your problem??
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.