简体   繁体   中英

PHP MySQL query with condition

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

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

您是说MAX_WEIGHT >= $weight吗?

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM