Chloë E.
Chloë E.

Reputation: 33

Remove duplicate MySQL result of query?

I am using SELECT COUNT to check if my mysql table has any duplicates, and when it does, I want it to remove the duplicate.

Here is a screenshot of how the query result looks like: http://s.prelicio.us/lDyRV.png

Now there are 2 'Aanpassingen voor website Chat-Garden' in my table. That is correct, so I've made it count and where 'Aantal' stands, that's where it'll tell me how many rows have that 'Aanpassingen voor website Chat-Garden'.

But now I want it to remove the duplicate row in my screenshot, since the '2' already has been shown at 'Aantal'. I then do not need the extra row in my screenshot. (excuse me for my bad english)

This is de current code:

<?php 

$products = mysql_query('SELECT * FROM `preo_invoices-products`');
if(!$products || mysql_num_rows($products) == '0') echo '<li>Er zijn geen producten voor dit factuur.</li>'; 

else $i == '0'; while($product = mysql_fetch_object($products)) {

$color = ($i % 2 == 0) ? ' class="grey"' : ''; $i++;


$result = mysql_query('SELECT *, COUNT(`id`) FROM `preo_invoices-products` WHERE `invoiceid` = "'.$paid->id.'" GROUP BY `description`');
while($row = mysql_fetch_array($result)) { 

?>

<li<?php echo $color; ?>>

<span class="inv9"><?php echo $row['COUNT(`id`)']; ?></span> // This one is the 'Aantal' where it shows 2
<span class="inv3">

<?php 
$services = mysql_query('SELECT * FROM `preo_services` WHERE `id` = "'.$product->serviceid.'" LIMIT 1');
while($service = mysql_fetch_object($services)) echo strtoupper($service->name); 
?>

</span>
<span class="inv14"><?php echo $product->description; ?></span>
<span class="inv9"><?php echo $product->tax; ?>%</span>
<span class="inv12">&euro;<?php echo $product->sum; ?>,-</span>
<span class="inv15">&euro;<?php echo $product->sum*$row['COUNT(`id`)']; } ?>,-</span>

</li>

<?php } ?> 

It now actually has to remove the duplicate row, since the 'Aantal' shows me I have a duplicate one. But when I have 3 same rows, it'll show '3' at Aantal, and still has to show 1 row at my screenshot.

Any ideas?

Upvotes: 2

Views: 7684

Answers (1)

John Woo
John Woo

Reputation: 263933

you can add DISTINCT in your query, example

SELECT DISTINCT * FROM tableName

Upvotes: 7

Related Questions