简体   繁体   中英

Combined Where Clause in Mysql PHP Query

I have 2 table called Classifieds and Classifieds_meta below is the screenshot of classifieds_meta table

CLICK TO SEE THE SCREENSHOT

im writing a search filter function with muliple where clause like

SELECT *,classifieds.id as id 
FROM classifieds,classifieds_meta 
WHERE (category=2 OR category=3 OR category=4 OR category=5) 
AND ((meta_key='vehicle_make' and meta_value=3 OR meta_key='vehicle_make' and meta_value=4)
AND meta_key='vehicle_mileage' and meta_value=9 OR meta_key='vehicle_mileage' and meta_value=10 OR meta_key='vehicle_mileage' and meta_value=11 ) 
AND classifieds.id=classifieds_meta.classifieds_id 
GROUP BY classifieds.id

But the above sql statement IGNORES vehicle_make , meta_value and meta_key field condition, and displays incorrect result, what i want to achieve exactly is i want to get vehicles where category is in (2,3,4 or 5) and meta_key is vehicle_make and meta_value is 3 or 4 and meta_key is vehicle_mileage and meta_value is 9,10 or 11.

can someone please help me to form better SQL statement to get right result

I suppose you are looking for vehicles where category is 2,3,4 or 5 and (meta_key is vehicle_make and (meta_value is 3 or 4)) or (meta_key is vehicle_mileage and (meta_value is 9,10 or 11)).

In this case the query should be:

SELECT *,classifieds.id as id
FROM classifieds,classifieds_meta 
WHERE (category=2 OR category=3 OR category=4 OR category=5) 
AND  ((meta_key='vehicle_make' AND ( meta_value=2 OR meta_value=4)) OR (meta_key='vehicle_mileage' AND (meta_value=9 OR meta_value=10 OR meta_value=11)))

Use parenthesis to make your statement understandable and finally use the same parenthesis in your SQL query.

EDIT:

If you want to use IN the query would be:

SELECT *,classifieds.id as id
FROM classifieds,classifieds_meta 
WHERE category IN (2, 3, 4, 5) 
AND  ((meta_key='vehicle_make' AND  meta_value IN (2,4)) OR (meta_key='vehicle_mileage' AND meta_value IN (9, 10, 11)))

I think you are looking for a having clause instead of a where . You are trying to look for conditions across multiple rows:

SELECT *, c.id as id 
FROM classifieds c join
     classifieds_meta  cm
     on c.id = cm.classifieds_id 
GROUP BY c.id
HAVING sum(meta_key = 'vehicle_make' and meta_value in (3, 4)) > 0 and
       sum(meta_key = 'vehicle_mileage' and meta_value in (9, 10, 11)) > 0 and
       sum(category in (2, 3, 4, 5)) > 0

The > 0 is saying that at least one row matches each condition.

Note that I also fixed the join to be use explicit join syntax.

This is essentially what you have (excluding table connection and category filter):

((meta_key='vehicle_make' and meta_value IN (3,4)) AND meta_key='vehicle_mileage' and meta_value=9)
    OR 
(meta_key='vehicle_mileage' and meta_value IN (10, 11))

Therefore the vehicle_make portion is impossible due to meta_value NEVER being both (3 OR 4) AND 9.

I think what you're going for is:

(meta_key='vehicle_make' and meta_value IN (3,4))
    OR 
(meta_key='vehicle_mileage' and meta_value IN (9, 10, 11))

All together now:

WHERE classifieds.id=classifieds_meta.classifieds_id AND category IN (2, 3, 4, 5)
AND (
    (meta_key='vehicle_make' and meta_value IN (3,4))
        OR 
    (meta_key='vehicle_mileage' and meta_value IN (9, 10, 11))
)

The takeaway from this is operator precedence. AND pairs get evaluated before OR.

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