Mysql 过滤订单,只加入我通过复选框选择的项目
MYSQL 我想根据表单复选框中的项目筛选“订单” 比如说,, 我查过了 A项+B项,但不包括C项 订单1 项目A 订单2 B项 订单3 项目A B项 项目C 订单4 项目A B项 我想要的是: 订单1、订单2和订单4,但不包括订单3,因为这也包含项目C 我的问题是这样的Mysql 过滤订单,只加入我通过复选框选择的项目,mysql,join,checkbox,filter,Mysql,Join,Checkbox,Filter,MYSQL 我想根据表单复选框中的项目筛选“订单” 比如说,, 我查过了 A项+B项,但不包括C项 订单1 项目A 订单2 B项 订单3 项目A B项 项目C 订单4 项目A B项 我想要的是: 订单1、订单2和订单4,但不包括订单3,因为这也包含项目C 我的问题是这样的 SELECT P.id as product_id,P.name as product_name, O.id as order_id,O.group_id,O.payment_date,O.payment_type_id,I.
SELECT
P.id as product_id,P.name as product_name,
O.id as order_id,O.group_id,O.payment_date,O.payment_type_id,I.quantity,
M.name,M.surname,M.email
FROM tbl_orders O
LEFT JOIN tbl_order_items I on I.order_id = O.id
LEFT JOIN tbl_products P on P.id = I.product_id
LEFT JOIN tbl_members M on M.id = O.member_id
WHERE
I.product_id in (1044,1129,20976,16775)
AND
O.status_id = 311 and O.payment_status_id = 349
谢谢我猜核心问题是您想排除具有任何未检查值的订单,并包括至少具有一个已检查值的订单
SELECT
P.id as product_id,P.name as product_name,
O.id as order_id,O.group_id,O.payment_date,O.payment_type_id,I.quantity,
M.name,M.surname,M.email
FROM tbl_orders O -- start with orders
-- add in the items, but use INNER to avoid orders that do not have
-- at least one order_item whose id was checked
INNER JOIN tbl_order_items I on I.order_id = O.id
AND I.product_id in (1044,1129,20976,16775)
-- left join in the items which were NOT checked. If there are any, the
-- rows will have bad product ids in them.
-- But if there ARE NOT any bad items in the order,
-- then there will only be one row with NULL for BADITEMS.product_id
LEFT JOIN tbl_order_items BADITEMS on BADITEMS.order_id = O.id
AND BADITEMS.product_id NOT in (1044,1129,20976,16775)
-- Pull in the product information
INNER JOIN tbl_products P on P.id = I.product_id
-- Pull in member information
LEFT JOIN tbl_members M on M.id = O.member_id
WHERE
-- the inner join has already insured that we only get orders
-- that have checked items.
-- To remove the orders that ALSO had unchecked items,
-- We take only rows where the BADITEMS join failed.
BADITEMS.product_id IS NULL
AND
O.status_id = 311 and O.payment_status_id = 349
您的查询与您的示例有何关联?这是逻辑,只是一个示例。