我有一家定制店,我需要重新发货。但是,有时会晚一些,同时,当购物车仅包含特定范围的产品时,我需要添加送货选项。
所以有一个ship_method表
id menuname name zone maxweight
1 UK Standard ukfirst 1 2000
2 UK Economy uksecond 1 750
3 Worldwide Air world_air 4 2000
为此,我添加了另一列 prod_restrict,其中 0 表示现有产品,1 表示受限产品,以及一个名为 Ship_prod_restrict 的新表,其中包含两列,ship_method_id 和 item_id,列出运输类别中允许的产品。 因此,我需要做的就是查看我的交易,对于每个购物车,只需检查哪些运输方式的 prod_restrict 为 0 或为 1,并且购物车中没有不在限制表中的产品。
不幸的是,由于您无法将外部查询的值转换为内部查询,因此我找不到一种简洁的方法来执行此操作。 (由于下面的评论,已编辑以显示完整的查询)
select ship_method.* from ship_method, ship_prod_restrict where
ship_method.`zone` = 1 and prod_restrict='0' or
(
prod_restrict='1'
and ship_method.id = ship_prod_restrict.ship_method_id
and (
select count(*) from (
select transactions.item from transactions
LEFT JOIN ship_prod_restrict
on ship_prod_restrict.item_id = transactions.item
and ship_prod_restrict.ship_method_id=XXXXX
where transactions.session='shoppingcartsessionid'
and item_id is null
) as non_permitted_items < 1 )
group by ship_method.id
为您提供该部分是否匹配的列表,并作为内部查询使用,但我无法在其中获取该ship_method_id(位于XXXXX)。
是否有一种简单的方法可以做到这一点,或者我的做法是错误的?我目前无法更改主要发货表,因为它目前已经就位,但其他部分可以更改。我也可以在 PHP 中做到这一点,但你知道,这看起来像是作弊!
最佳答案
不确定计数有多重要,但这可能会轻一些 - 如果没有完整的表架构转储,很难判断:
SELECT COUNT(t.item) FROM transactions t
INNER JOIN ship_prod_restrict r
ON r.item_id = t.item
WHERE t.session = 'foo'
AND r.ship_method_id IN (**restricted, id's, here**)
关于Mysql:将产品限制运输选项添加到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8145103/