删除sql join中的空值和sql中的union操作符

hfwmuf9z  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(455)

[已解决]如何仅按字段提供程序的顺序删除空值,在我的示例中,该表使用联接表和联合
这是我的数据库模式和sql查询
http://sqlfiddle.com/#!9/f77862/2号楼
为了清楚地描述这里我附上图片
红线圈是应该去掉的

3df52oht

3df52oht1#

不能仅删除行中的该字段。但是,只需使用where子句过滤掉具有空值的记录,就可以除去整行。
http://sqlfiddle.com/#!9/f77862/42电话

SELECT
    m.provider_id,
    m.provider_name,
    p.purchase_order_code,
    NULL AS purchase_order_sample_code,
    p.provider_id AS provider_order
FROM mst_provider AS m
LEFT JOIN trx_purchase_order AS p
    ON (m.provider_id = p.provider_id)
WHERE p.provider_id != NULL
UNION
SELECT
    m.provider_id,
    m.provider_name,
    p.purchase_order_code,
    NULL AS purchase_order_sample_code,
    p.provider_id AS provider_order
FROM mst_provider AS m
RIGHT JOIN trx_purchase_order AS p
    ON (m.provider_id = p.provider_id)
UNION
SELECT
    m.provider_id,
    m.provider_name,
    NULL,
    s.purchase_order_sample_code,
    s.provider_id
FROM mst_provider AS m
RIGHT JOIN trx_purchase_order_sample AS s
    ON (s.provider_id = m.provider_id)
zbsbpyhn

zbsbpyhn2#

我假设你想过滤提供者的顺序是空的?
我将整个查询 Package 为一个子查询,并在末尾放置一个where子句,只返回provider\u order不为null的记录。
http://sqlfiddle.com/#!9/f77862/50/0号

SELECT *
FROM (
    SELECT m.provider_id,
        m.provider_name,
        p.purchase_order_code,
        NULL AS purchase_order_sample_code,
        p.provider_id AS provider_order
    FROM mst_provider AS m
    LEFT JOIN trx_purchase_order AS p
        ON (m.provider_id = p.provider_id)

    UNION

    SELECT m.provider_id,
        m.provider_name,
        p.purchase_order_code,
        NULL AS purchase_order_sample_code,
        p.provider_id AS provider_order
    FROM mst_provider AS m
    RIGHT JOIN trx_purchase_order AS p
        ON (m.provider_id = p.provider_id)

    UNION

    SELECT m.provider_id,
        m.provider_name,
        NULL,
        s.purchase_order_sample_code,
        s.provider_id
    FROM mst_provider AS m
    RIGHT JOIN trx_purchase_order_sample AS s
        ON (s.provider_id = m.provider_id)
    ) _temp
WHERE provider_order IS NOT NULL
uqdfh47h

uqdfh47h3#

“删除”是什么意思?删除整行或只是按顺序显示除null以外的其他内容。如果以后要查找,那么可以使用coalesce
编辑:仅对小提琴进行了更改:http://sqlfiddle.com/#!9/f77862/46电话

相关问题