mysql中不是IN运算符返回NULL

时间:2016-07-15 13:07:52

标签: mysql

                SELECT *
                FROM fabric_restock AS fabr
                LEFT JOIN fabric_po AS fabpo 
                ON fabpo.fab_id = fabr.fab_id
                WHERE 
                fabr.fab_id NOT IN (     
                SELECT fabpo.fab_id
                FROM 
                fabric_po fabpo
                WHERE
                fabpo.status <= 1
                AND 
                fabpo.fab_id IS NOT NULL
                )
                AND 
                fabr.restock_ten <= fabpo.qty

//当子查询没有返回任何行或返回空值时,完整查询返回null

我该如何解决这个问题?

当子查询没有返回记录时,它应该返回外部查询的所有记录

编辑:原始查询:

                SELECT *
                FROM fabric_restock AS fabr
                LEFT JOIN fabric_po AS fabpo ON fabpo.fab_id = fabr.fab_id
                WHERE 
                fabr.fab_id NOT IN (
                SELECT fabpo.fab_id
                FROM 
                fabric_po fabpo
                JOIN 
                fabric_challan fabchallan
                ON 
                fabpo.po_num =  fabchallan.po_num
                WHERE
                fabchallan.challan_num 
                NOT IN (SELECT challan_num FROM fabric_grn)
                AND 
                fabpo.status <= 1
                )
                AND 
                fabr.restock_ten <= fabpo.qty

示例数据:

    # fab_id, fabric_mother_id, qty_mother, cluster, qty, cf_thirty, cf_ten, cf_three, cf_cluster, sellout_thirty, sellout_ten, sellout_three, num_good_sku, num_coll, num_collection_p, num_sale, new_cf, percentile, restock_thirty, restock_ten, restock_three, status, num_new, parent, lace_type, po_id, po_num, fab_id, vendor_code, color_code, qty, estimated_price, po_date, arrival_date, estimated_date, composition, material, type, inType, status, gsm, notes, image, approveStatus, approve_date, shrinkage, fabric_mother_id, qty_type, main_construction, place_order_date, width, vendor_comments, email_send, wash, brand
FAB1001, SBL104, 10.00, cluster, 10.00, 10.00, 10.00, 10.00, 10, 10.00, 10.00, 10.00, 10, 10, 10, 10, 10.00, 10.00, 10.00, 10.00, 10.00, 0, 10, 1, Washed, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , 
FAB1002, SBL101, 20.00, cluster1, 20.00, 20.00, 20.00, 20.00, 20, 20.00, 20.00, 20.00, 20, 20, 20, 20, 20.00, 20.00, 20.00, 20.00, 20.00, 0, 20, 2, Bleached, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , 
FAB1003, SBL102, 30.00, cluster2, 30.00, 30.00, 30.00, 30.00, 30, 30.00, 30.00, 30.00, 30, 30, 30, 30, 30.00, 30.00, 30.00, 30.00, 30.00, 0, 30, 3, RFD, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , 
FAB1004, SBL103, 40.00, cluster3, 40.00, 40.00, 40.00, 40.00, 40, 40.00, 40.00, 40.00, 40, 40, 40, 40, 40.00, 40.00, 10.00, 10.00, 10.00, 0, 40, 4, Dyed, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , 

fabpo表:此刻为空

1 个答案:

答案 0 :(得分:1)

你正在加入一张空桌子。没关系。

但是后来你试图将表中的值与连接表中的空字段进行比较,这不起作用。

fabr.restock_ten <= fabpo.qty
  

fabpo表:此刻为空

我猜你可以通过在fabpo.qty上检查null来处理这种情况,虽然它有点奇怪(可能只是因为我并不真正理解所有列名的含义)。

AND (fabr.restock_ten <= fabpo.qty OR fabpo.qty IS NULL)