Mysql join 给出重复的行 [英] Mysql join gives duplicate rows

查看:49
本文介绍了Mysql join 给出重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个表,我正在使用 join 从这 2 个表中获取公共记录.我使用了以下查询,但我的问题是我的记录翻了一番.查询如下

I have 2 tables and i am using join to get common records from those 2 tables. i have used the following query but my problem is i am getting the records doubled. The query is as follows

SELECT * FROM pos_metrics pm INNER JOIN pos_product_selling pps ON   
pm.p_id=pps.p_id WHERE pm.p_id='0' AND pps.pos_buying_id='0' AND pm.type=1

pos_metrics 表:

pos_metrics table:

pos_product_sales 表:

pos_product_selling table:

输出:

编辑
当我尝试将 GROUP BY 和 DISTINCT 一起使用时,我没有得到重复项,但第二个表中的值重复了.还有其他解决方案吗?

EDIT
When I tried to use GROUP BY and DISTINCT together I am not getting duplicates but the value from the second table is repeated. Any other solutions ?

推荐答案

pos_metrics表中添加一个主键并引入到pos_product_sales表中,然后做一个JOIN 基于主键以及其他条件.那样你就不会得到这些重复项了.

Add a primary key in the pos_metrics table and introduce it to the pos_product_selling table, then do a JOIN based on the primary key as well as the other criteria. You won't get these duplicates then.

这里有重复的原因是因为不可能根据值对两个表进行唯一比较.

The reason you have duplicates over here is because there is no possibility of an unique comparison to be done on both tables based on a value.

这篇关于Mysql join 给出重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆