Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I stop duplicates in Mysql results?

Posted on 2010-11-23
7
Medium Priority
?
438 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 800 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 800 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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

926 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