开发者

MySQL Multiple Where Conditions on Same Field in a Join

I have a table called "orders" with

order_id
customer_name
item

and am joining it to another table called "orders_requests" containing

order_id
requests

One row in "orders" may have zero or more rows in "order_requests." I'm trying to perform a query to find "orders" that have "orders_requests" of "rush processing" and "free shipping." I'm leaning towards a RIGHT OUTER JOIN with the "orders_requests" table on the left so it only returns results if a corresponding "orders" row exists, but I've had no success with either of these queries.

This one doesn't work since it pulls rows that are rush processing or free shipping.

SELECT DISTINCT orders.order_id, orders.* , 
FROM orders_requests
RIGHT OUTER JOIN orders ON orders_requests.order_id = orders.order_id
WHERE orders_requests.requests
IN ('rush processing', 'free shipping')

And this one clearly doesn't work since it's impossible for any one row to match both conditions.

SELECT DISTINCT orders.order_id, orders.* , 
FROM orders_requests
RIGHT OUTER JOIN orders ON orders_requests.order_id = orders.order_id
WHERE orders_requests.requests = 'rush processing'
AND orders开发者_如何学JAVA_requests.requests = 'free shipping'

Is what I'm trying to do possible with my table setup do I need to change the structure?


I am not sure if this would work since I cannot test it atm but try this:

select a.order_id, b.*, c.*
from orders a 
left outer join orders_requests b on b.order_id = a.order_id
left outer join orders_requests c on c.order_id = a.order_id
where b.requests = 'rush processing' and c.requests = 'free shipping'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜