1

I have found questions related to removing duplicates and counting duplicates, but I haven't been able to add a duplicate count to MySQL rows, so I pose this question. Here's what I have so far:

$query = "SELECT * FROM Losers ORDER BY Date DESC"; 
$result = mysql_query($query) or die(mysql_error());
echo '<table>';
while($row = mysql_fetch_array($result)){
    echo '<tr><td>' . $row['Name']. "</td><td>". date("l, M j, Y",strtotime($row['Date'])) . '</td></tr>';
}
echo '</table>';
mysql_close($con);

Now I want to include a count of the duplicates in Name for each row. So, it would read something like this:

Ben 2
Laura 3
Amy 1
Laura 3
Ben 2
Laura 3

I found a this query to group duplicates in Name and count them:

$newQuery = "SELECT Name, Date, COUNT(Name) FROM Losers GROUP BY Name"; 

But then it will output:

Ben 2
Laura 3
Amy 1

How do I incorporate both queries?

5 Answers 5

4

Afaik, you can't do that without a subquery:

SELECT Losers.*, (SELECT COUNT(*) FROM Losers AS Sub WHERE Sub.Name = Losers.Name) AS `count` FROM Losers ORDER BY Date DESC

But personally, I'd rather do that with two separate queries instead.

Edit: see the two answers below, they're much better.

Sign up to request clarification or add additional context in comments.

3 Comments

This would get exponentially slow as a function of entries in Losers table.
Not to brag, but my answer or Basti's is better since the subquery will only be run once ;)
You're right. In fact I didn't even know that JOIN (SELECT...) is a valid statement. Stupid me )
4
SELECT Losers.Name, Losers.Date, L.Count
FROM Losers
JOIN (
    SELECT Name, COUNT( Name ) AS Count
    FROM Losers
    GROUP BY Name
) AS L ON Losers.Name = L.Name

Comments

3

The easiest way is probably to take your query with the GROUP BY clause and use it as a subquery that you join to your original table.

I.E.

SELECT L.*, Loser_Count.count
FROM Losers L
JOIN (SELECT loser_id, COUNT(Name) count FROM Losers GROUP BY Name) Loser_Count ON
  L.loser_id = Loser_Count.loser_id 
ORDER BY L.date DESC

This should work, but I wouldn't be surprised if a SQL genius comes back with a more efficient way. Hope this helps!

Comments

1

I would do it in two separate queries:

SELECT Name, COUNT(*) FROM Losers GROUP BY Name
/* Parse this to have a php array $l_count in form ("Ben" => 2, "Laura" => 3) */
SELECT Name FROM Losers

while ($name = mysql_fetch...) {
    print $name.' '.$l_count[$name]."\n";
}

Good luck!

Comments

-1

Using SQL

SELECT field 1, field 2, field 3, COUNT(*)
FROM table
GROUP BY field 1, field 2, field 3

That SQL will give you all distinct rows, and an output of # of times they show up

1 Comment

And that is exactly what he did and did not want to have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.