MySQL 两表字段值对比

默认分类 2017-05-21 20:53 暂无评论

场景:查询未充值有兑换记录的用户 ID

两表 t1 是兑换表, t2 是充值表不考虑其他字段内容,只对比两表的记录并且去重

使用 LEFT JOIN 来实现对比,DISTINCT 去重复,最后返回的结果是 t2 结果为 NULL 的显示。

SELECT DISTINCT
    t1.user_number,
    t2.user_number
FROM
    (
        SELECT DISTINCT
            t1.user_number
        FROM
            t_card_list t1
    ) t1
LEFT JOIN (
    SELECT DISTINCT
        t2.user_number
    FROM
        t_recharge_log t2
    WHERE
        t2.type = 10
) t2 ON t1.user_number = t2.user_number
WHERE
    t2.user_number IS NULL;
暂无评论