Mysql join 给出重复的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6352454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mysql join gives duplicate rows
提问by Deepak
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
我有 2 个表,我正在使用 join 从这 2 个表中获取公共记录。我使用了以下查询,但我的问题是我的记录翻了一番。查询如下
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 table:
pos_metrics 表:
pos_product_selling table:
pos_product_sales 表:
Output:
输出:
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 ?
编辑
当我尝试将 GROUP BY 和 DISTINCT 一起使用时,我没有得到重复项,但第二个表中的值重复了。任何其他解决方案?
采纳答案by Shyam Natraj Kanagasabapathy
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.
在pos_metrics
表中添加一个主键并引入到pos_product_selling
表中,然后JOIN
根据主键以及其他条件做一个。那样你就不会得到这些重复项了。
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.
您在这里有重复的原因是因为不可能根据值对两个表进行唯一比较。
回答by Jayakrishnan K
Try something like these
尝试这样的事情
GROUP BY pos_product_selling.metrics
回答by nunu
try this:
尝试这个:
SELECT DISTINCT * FROM ...
GROUP BY pm.metrics
回答by Denis de Bernardy
To eliminate dups, use distinct:
要消除重复,请使用 distinct:
select distinct * from ...
But I've a feeling your question is about something else -- and that you'd need to post the specific code for more help.
但是我感觉您的问题是关于其他问题的 - 您需要发布特定代码以获得更多帮助。
回答by gmhk
SELECT * FROM pos_metrics pm, pos_product_selling pps
Where pm.p_id=pps.p_id AND pm.p_id='0' AND pps.pos_buying_id='0' AND pm.type=1
try the above query
试试上面的查询