mysql查询时有两条一模一样的结果应该只显示一条

场景:使用左连接关联查询时,发现查询的结果中有两条一模一样的数据,因此,需要对查询结果去重。

解决方案:在具有唯一索引的字段前加上DISTINCT关键字即可。

在加关键字之前的SQL:

SELECT 
          a.order_id,a.user_id,a.biz_price,a.start_effect,a.end_effect,a.order_type, a.pay_time, a.write_off_lastdate,
        a.write_off_status, b.store_img, b.store_name, a.order_status, a.pay_status,c.type_name,d.biz_name,cp.coupon_name
        FROM
          tb_service_order a
        LEFT JOIN tb_service_store  b ON a.store_code = b.store_code
        LEFT JOIN tb_coupon_type c ON c.id = a.order_type
        LEFT JOIN tb_service_merchant_biz d ON d.biz_code = a.biz_code
        LEFT JOIN tb_coupon cp ON cp.coupon_type_id = c.id
        WHERE 
         a.user_id  = 7860

查询结果:

 加过关键字之后的SQL:

SELECT 
      DISTINCT    a.order_id,a.user_id,a.biz_price,a.start_effect,a.end_effect,a.order_type, a.pay_time, a.write_off_lastdate,
        a.write_off_status, b.store_img, b.store_name, a.order_status, a.pay_status,c.type_name,d.biz_name,cp.coupon_name
        FROM
          tb_service_order a
        LEFT JOIN tb_service_store  b ON a.store_code = b.store_code
        LEFT JOIN tb_coupon_type c ON c.id = a.order_type
        LEFT JOIN tb_service_merchant_biz d ON d.biz_code = a.biz_code
        LEFT JOIN tb_coupon cp ON cp.coupon_type_id = c.id
        WHERE 
         a.user_id  = 7860

查询结果:

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iamlzjoco

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值