Solved

How do I stop duplicates in Mysql results?

Posted on 2010-11-23
7
433 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
Independent Software Vendors: 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 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do i use the await event in php echo 5 44
How do I Check for duplicate entries in mysql 15 50
PHP substring 3 54
How do I init a PHP page jusing JQuery? 8 32
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
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.

710 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