473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

MySQL, SELECTing items with GROUP BY, HAVING COUNT(*) > 1

Greetings!

I've recently been trying to do something, which apparently looks like
it may be a little odd.. I'm not finding anything in the manuals or
anywhere on the web where something similiar is being done.. so it
looks like my two options now are to post for some assistance, or find
another way to accomplish my task.

Basically, what I'm trying to do is select the number of duplicate
entries from a table using three columns. There is one primary key with
the table (and that seems to be what's throwing me off). Here's an
example:

mysqlSELECT *, COUNT(*) FROM tbBillingLineItems GROUP BY user_id,
pull_start, item_type HAVING COUNT(*) 1;
+---------+---------+------------+-----------+--------+-----------------+-------------+------------+-------+----------+
| item_id | user_id | pull_start | item_type | amount | amount_per_item
| total_items | free_items | notes | COUNT(*) |
+---------+---------+------------+-----------+--------+-----------------+-------------+------------+-------+----------+
| 52 | 1 | 1157068800 | 1 | 0.00 | 0.01
| 2 | 5 | NULL | 8 |
| 53 | 1 | 1157068800 | 2 | 0.19 | 0.01
| 24 | 5 | NULL | 8 |
| 54 | 1 | 1157068800 | 3 | 10.00 | 10.00
| 1 | 0 | NULL | 8 |
| 55 | 2 | 1157068800 | 1 | 11.00 | 0.10
| 115 | 5 | NULL | 8 |
| 56 | 2 | 1157068800 | 2 | 4.60 | 0.02
| 236 | 5 | NULL | 8 |
| 51 | 2 | 1157068800 | 3 | 5.00 | 5.00
| 1 | 0 | NULL | 8 |
+---------+---------+------------+-----------+--------+-----------------+-------------+------------+-------+----------+
6 rows in set (0.00 sec)

What I'm looking to do, is pull the eight items that are being
matched.. I need to delete all but one copy of them. This is used in
the event that an application is run multiple times, it will create too
many entries into the database which need to be removed. Since the
primary key cannot be used to determine unique values, what I use
instead is the timestamp in which the entries are being run for, the
type of item it is, and the user_id the item is for.

In essence, what I'm looking for is something to delete all duplicate
entries from the table, based on user_id, pull_start, and item_type. If
I can figure out a way to get the item_id for each of these items that
were deemed duplicate (as indicated by the resulting COUNT(*)), then I
can delete them from there with a temporary table or a simple script.
One way to accomplish this that I've found was to repeat execution of
this query multiple times until there are no more duplicate items
returned.. but I'm sure there has to be a more efficient way.. I'm open
to any and all suggestions.

Thanks!
Michael Martinek

Oct 10 '06 #1
1 17222
Here is my scripted solution, for anyone who might be interested or
possibly running into the same wall as I am.

<?php

mysql_connect('dbHost','dbUser','dbPassword') or die('Unable to connect
to database.');
mysql_select_db('dbName') or die('Unable to select database.');

$sSQL = 'CREATE TEMPORARY TABLE dupes SELECT *, COUNT(*) - 1 AS cnt
FROM'
.' tbBillingLineItems GROUP BY user_id, pull_start, item_type'
.' HAVING COUNT(*) 1 ORDER BY item_id';
mysql_query($sSQL);

$sSQL = 'SELECT * FROM dupes';
if ($mRes = mysql_query($sSQL)) {
while ($mRow = mysql_fetch_assoc($mRes)) {
$sSQL = sprintf('DELETE FROM tbBillingLineItems WHERE
user_id = %u AND pull_start = %u AND item_type = %u LIMIT %u',
$mRow['user_id'], $mRow['pull_start'],
$mRow['item_type'], $mRow['cnt']
);
mysql_query($sSQL);
}
mysql_free_result($mRes);
}

mysql_close();
?>
Of course, I'd prefer not to have to do it in a fashion like this.. but
if I have to, ah well. At least it works.

Oct 10 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
2
by: Matik | last post by:
Hello everyone, Small and (I think) very simple quesiton;-) which makes me creazy. Let's say I have two tables listed below: T1 ==== IDX ==== 1
5
by: andreas.muller | last post by:
Hello everyone, I'm trying to solve this problem but can't seem to figure out how to start. I would like to create a rating system where people can vote (1-5 stars) on randomly displayed items....
1
by: sneha123 | last post by:
There will be some 20 questions and for each question there will be 4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net pls help me we...
2
by: ericv | last post by:
I have 3500 records in a table - each record has a unique value (KEY_ID field), but some records share the same value (in a field called POLE_ID) So, there may be 3 records that have the POLE_ID...
1
by: renfrochris | last post by:
I'm having difficulty finding the correct syntax that will allow me to select a group of invoices based on the total of an amount column located in its line items. Below are simplified examples of...
2
by: Catch_22 | last post by:
Hi, I have a stored procedure that has to extract the child records for particular parent records. The issue is that in some cases I do not want to extract all the child records only a...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.