Solved

How do I stop duplicates in Mysql results?

Posted on 2010-11-23
7
434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

627 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