0
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 的使用?

4

2 回答 2

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条目适合的地方加入。

===编辑===

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

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

认为第一个选项可能稍微优化一些,但完全有可能它们可以被查询计划器优化为相同。您必须运行一些基准测试才能确定。

===编辑 2===

所以现在我认为你要的是一个 INTERSECTION?

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)
) 
于 2013-08-28T09:04:56.410 回答
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 (...);
于 2013-08-28T09:05:59.823 回答