关系查询,不确定如何使用EXISTS构建它

时间:2013-08-28 09:00:07

标签: mysql

carts
id user_id

cart_proucts
id cart_id product_id price

products
much details about the product...

所以这是我目前表格结构的一种简单形式。我有一个产品ID列表,我需要检查用户是否有他们,我知道通过查看购物车属于用户的cart_products,如上所述。如果他有,我应该检索他所包含的“用户”和信息。但我还需要检查产品的价格范围,即产品的价格范围是否必须低于500,即cart_products中的价格列......

我怎样才能最简单地构建这个sql?我在考虑JOINS和一些EXISTS或IN的使用吗?

2 个答案:

答案 0 :(得分:0)

你想要这样的东西吗?

SELECT * FROM Users 
INNER JOIN carts on carts.user_id = Users.id
INNER JOIN cart_products on carts.id = carts_products.cart_id
WHERE cart_products.price > X and cart_products.price < Y
AND cart_products.product_id IN (LIST OF PRODUCT IDS)

INNER JOIN只会加入适合的cartscarts_products条目。

=== EDIT ===

要返回单个用户,您可以将第一行更改为:

SELECT DISTINCT Users.* FROM Users
INNER JOIN carts on carts.user_id = Users.id
INNER JOIN cart_products on carts.id = carts_products.cart_id
WHERE cart_products.price > X and cart_products.price < Y
AND cart_products.product_id IN (LIST OF PRODUCT IDS TO INCLUDE)
AND cart_products.product_id NOT IN (LIST OF PRODUCT IDS TO EXCLUDE)

或者在子查询中使用IN:

SELECT u.* from Users u WHERE u.id IN 
(
    SELECT Users.id FROM Users 
    INNER JOIN carts on carts.user_id = Users.id
    INNER JOIN cart_products on carts.id = carts_products.cart_id
    WHERE cart_products.price > X and cart_products.price < Y
    AND cart_products.product_id IN (LIST OF PRODUCT IDS)
)

THINK 第一个选项可能稍微更优,但完全有可能它们可以由查询规划器优化为相同。你必须运行一些基准来确定。

===编辑2 ===

所以现在我觉得你要求交叉?

SELECT DISTINCT Users.* FROM Users
INNER JOIN carts on carts.user_id = Users.id
INNER JOIN cart_products on carts.id = carts_products.cart_id
WHERE cart_products.price > X and cart_products.price < Y
AND cart_products.product_id IN (LIST OF PRODUCT IDS TO INCLUDE)

INTERSECT

SELECT DISTINCT Users.* FROM Users
INNER JOIN carts on carts.user_id = Users.id
INNER JOIN cart_products on carts.id = carts_products.cart_id
WHERE cart_products.price > X and cart_products.price < Y
AND cart_products.product_id NOT IN (LIST OF PRODUCT IDS TO EXCLUDE)

我认为也可能与......相同。

SELECT DISTINCT Users.* FROM Users
INNER JOIN carts on carts.user_id = Users.id
INNER JOIN cart_products on carts.id = carts_products.cart_id
WHERE cart_products.price > X and cart_products.price < Y
AND cart_products.product_id IN (LIST OF PRODUCT IDS TO INCLUDE)

EXCEPT

SELECT DISTINCT Users.* FROM Users
INNER JOIN carts on carts.user_id = Users.id
INNER JOIN cart_products on carts.id = carts_products.cart_id
WHERE cart_products.price > X and cart_products.price < Y
AND cart_products.product_id IN (LIST OF PRODUCT IDS TO EXCLUDE)

不确定哪些SQL实现允许EXCEPT,但另一种方法是使用LEFT JOIN,如下所述:http://en.wikipedia.org/wiki/Set_operations_(SQL)#EXCEPT_operator

===编辑3 ===

SELECT u.* from Users u WHERE u.id IN 
(
    SELECT Users.id FROM Users 
    INNER JOIN carts on carts.user_id = Users.id
    INNER JOIN cart_products on carts.id = carts_products.cart_id
    WHERE cart_products.price > X and cart_products.price < Y
    AND cart_products.product_id IN (LIST OF PRODUCT IDS TO INCLUDE)
)
AND u.id NOT IN
(
    SELECT Users.id FROM Users 
    INNER JOIN carts on carts.user_id = Users.id
    INNER JOIN cart_products on carts.id = carts_products.cart_id
    WHERE cart_products.price > X and cart_products.price < Y
    AND cart_products.product_id IN (LIST OF PRODUCT IDS TO EXCLUDE)
) 

答案 1 :(得分:0)

SELECT *
FROM `users` `u`
JOIN `carts` `c` ON (`u`.`id` = `c`.`user_id`)
JOIN `cart_products` `cp` ON (`cp`.`cart_id` = `c`.`id`)
JOIN `products` `p` ON (`p`.`id` = `cp`.`product_id` AND `p`.`price` > `cp`.`price`)
WHERE `p`.`id` IN (...);