Solved

How do I stop duplicates in Mysql results?

Posted on 2010-11-23
7
429 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 142

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 142

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now