Mysql join gives duplicate rows

42,783

Solution 1

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.

Solution 2

Try something like these

GROUP BY pos_product_selling.metrics

Solution 3

try this:

SELECT DISTINCT * FROM ...
GROUP BY pm.metrics

Solution 4

To eliminate dups, use 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.

Solution 5

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

Share:
42,783
Deepak
Author by

Deepak

Currently employed as a PHP Developer and I have 6 Years of experience in PHP with the framework knowledge on Codeigniter-2. I also worked on Java projects and I have 2 years of experience in Java. I would like to learn good techniques in programming and as I live in So I learn new things everyday. Recently I attempted my own framework called Mercury

Updated on September 24, 2021

Comments

  • Deepak
    Deepak over 2 years

    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 table:
    enter image description here

    pos_product_selling table: enter image description here

    Output:

    enter image description here

    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 ?

  • Denis de Bernardy
    Denis de Bernardy almost 13 years
    My feeling is you need a group by/having somewhere, but your question is lacking context.
  • Denis de Bernardy
    Denis de Bernardy almost 13 years
    Well, yeah, that's the expected result. Shouldn't you be using something like: select distinct pm.* from ...?
  • Deepak
    Deepak almost 13 years
    this is working but is this the right way to do ?? i am dealing with thousands of record here..
  • Ricky Boyce
    Ricky Boyce almost 9 years
    What do you mean by "and introduce it to the pos_product_selling table"
  • Martin Prikryl
    Martin Prikryl over 8 years
    Can you comment your answer?
  • Favourite Onwuemene
    Favourite Onwuemene about 8 years
    @Ricky B He means create a column on the pos_product_selling table whose value is a primary key on pos_metrics. A typical name for such a column would be "pos_metrics_id"
  • ERIC
    ERIC about 3 years
    INNER JOIN may create unique rows with only one different column, so distinct may not reduce result.
  • kiamoz
    kiamoz about 3 years
    Answer was easy but not shown lol , thank you Jayak ;)