Solved

How do I stop duplicates in Mysql results?

Posted on 2010-11-23
7
432 Views
Last Modified: 2013-12-12
Experts,
How do I prevent dupicate results in my select statement?  I am attempting to assign a "category id" and a "parent id" to the category list.  There are hundreds of products with the same category and parent id.  I need to limit to 1 result for each.

Thanks!

Randy
mysql_query("TRUNCATE TABLE `OCcategory`");


$result5 = mysql_query("SELECT * FROM `temp3` ORDER BY `cat_id` LIMIT 1");

while($row5 = mysql_fetch_assoc($result5))
{
if (trim($row5['category'])=="") { continue;}
mysql_query("INSERT INTO OCcategory (category_id, parent_id) VALUES ('".$row5['cat_id']."','$zero')");

}

Open in new window

0
Comment
Question by:rlb1
7 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 34198698
I have written an article for this kind of problems ...
http://www.experts-exchange.com/A_3203.html

however, with mysql, the solutions are a bit trickier...
you might try this:
$result5 = mysql_query("SELECT * FROM `temp3` GROUP BY cat_id, parent_id ORDER BY `cat_id` ");

Open in new window

0
 

Author Comment

by:rlb1
ID: 34198971
OK, I have tried GROUP BY, DISTINCT, INSERT IGNORE, etc.  and I am still getting duplicates.  Any other thought as how to solve?

Thanks!
0
 

Author Comment

by:rlb1
ID: 34198995
Using
$result5 = mysql_query("SELECT * FROM `temp3` GROUP BY cat_id, parent_id ORDER BY `cat_id` ");
I got the following error.

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34199152
ok.
can you please show a data sample, and show the requested output from there?
0
 
LVL 2

Assisted Solution

by:benschwartz
benschwartz earned 200 total points
ID: 34199998
your query above (SELECT * FROM `temp3` GROUP BY cat_id, parent_id ORDER BY `cat_id`) looks right. Are you sure that 'temp3' exists and has those fields?
0
 
LVL 3

Assisted Solution

by:roynaufal
roynaufal earned 100 total points
ID: 34203350
I usually use select distinct....from
would be helpful if u can show us some of the data though
0
 

Author Comment

by:rlb1
ID: 34206885
Thanks for your help!  I got it to work!
$result20 = mysql_query("SELECT * FROM `ctgstorefeed` GROUP BY picturesku ORDER BY `picturesku`") or die( mysql_error() );

while($row20 = mysql_fetch_assoc($result20))
{

mysql_query("INSERT IGNORE INTO images (image) VALUES ('".$row20['picturesku']."')");

}

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question