该SQL语句是正确的,不会出现重复的行。\n\nsql\nSELECT\n\theader.id,\n\theader.create_by,\n\theader.create_time,\n\theader.doc_id,\n\theader.base_entry,\n\theader.order_no,\n\theader.ship_type,\n\theader.remark,\n\theader.doc_status,\n\theader.business_status,\n\theader.overseas_invoice_no,\n\theader.contact,\n\theader.address,\n\theader.tel,\n\titem.base_line_no,\n\titem.order_line_no,\n\titem.product_id,\n\titem.product_name,\n\titem.quantity,\n\titem.unit,\n\titem.demand_quantity,\n\titem.product_store_id,\n\titem.facility_id,\n\titem.product_store_id_to,\n\titem.facility_id_to,\n\titem.header_id,\n\tboh.user_id,\n\tboh.user_name,\n\theader.plan_delivery_time,\n\theader.is_invalid\nFROM\n\tdemand_delivery_command_header header\n\tLEFT JOIN demand_delivery_command_item item ON header.id = item.header_id\n\tLEFT JOIN bos_order_header boh ON boh.order_code = header.order_no\nWHERE\n\theader.plan_delivery_time < CURDATE()\nAND\n\titem.demand_quantity > 0\nAND\n\t(item.quantity-item.demand_quantity) > 0\nAND\n\t(header.is_invalid != 'Y' OR header.is_invalid is null)\nAND header.doc_status != '3'\nGROUP BY id\n\n\n分析:\n\n* 该 SQL 语句使用了 LEFT JOIN 将 demand_delivery_command_headerdemand_delivery_command_itembos_order_header 三张表关联起来,并使用 WHERE 子句对数据进行筛选。\n* WHERE 子句中使用 header.plan_delivery_time < CURDATE() 筛选出计划交付时间早于当前日期的数据。\n* item.demand_quantity > 0(item.quantity-item.demand_quantity) > 0 两个条件确保选取的记录满足需求量大于零且已交付数量小于总数量的条件。\n* (header.is_invalid != 'Y' OR header.is_invalid is null) 筛选出有效的数据。\n* header.doc_status != '3' 筛选出状态不是 3 的数据。\n* 最后使用 GROUP BY id 对结果进行分组,保证每条记录的 ID 是唯一的,避免出现重复的行。\n\n结论:\n\n该 SQL 语句通过使用 LEFT JOIN 和 WHERE 子句,并添加必要的过滤条件,保证了数据准确性和完整性,同时使用 GROUP BY id 避免出现重复的行。\n


原文地址: https://www.cveoy.top/t/topic/pMYQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录