mysql – 删除SQL中的SQL JOIN和UNION操作符中的NULL值

[求助]如何仅在字段provider_order中删除NULL值,在我的情况下,表使用JOIN表和UNION

这是我的数据库模式和我的SQL查询

http://sqlfiddle.com/#!9/f77862/2

为了清楚地描述这里我附上图片

The red line circle is which should be removed

最佳答案 您不能仅删除行中的该字段.但是,只需使用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)
点赞