Unix Technical Forum

How to find nested dupes

This is a discussion on How to find nested dupes within the MySQL forums, part of the Database Server Software category; --> I have a query problem. There can be dupes in machine - no problem There can be dupes in ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:15 AM
Jaak
 
Posts: n/a
Default How to find nested dupes

I have a query problem.

There can be dupes in machine - no problem
There can be dupes in owner - no problem

but I like to find out whether there are machine dupes of the same owner

this are the contents of the table

owner,machine
--------------------
michael,car
brad,car
jeniffer,laptop
john,car
john,car
john,laptop
brad,laptop


I like to do a query to john with result:
car

If i do the same to brad I get an empty result.

Can anybody help please me with this?

Thanks in advance


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:15 AM
Pedro Graca
 
Posts: n/a
Default Re: How to find nested dupes

Jaak wrote:
> There can be dupes in machine - no problem
> There can be dupes in owner - no problem
>
> but I like to find out whether there are machine dupes of the same owner


<snip content="table contents"/>

> I like to do a query to john with result:
> car
>
> If i do the same to brad I get an empty result.


Why don't you check john and brad and whoever else is in the database at
the same time?

> Can anybody help please me with this?


You want to

*GROUP BY*

the records

*HAVING*

more than 1 machine



mysql> desc foo;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| owner | varchar(10) | YES | | NULL | |
| machine | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
2 rows in set (0.09 sec)

mysql> insert foo values
-> ('michael', 'car'),
-> ('brad', 'car'),
-> ('jeniffer', 'laptop'),
-> ('john', 'car'),
-> ('john', 'car'),
-> ('john', 'laptop'),
-> ('brad', 'laptop');
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> select owner, machine
-> from foo
-> group by owner, machine
-> having count(machine) > 1;
+-------+---------+
| owner | machine |
+-------+---------+
| john | car |
+-------+---------+
1 row in set (0.02 sec)

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 07:15 AM
Jaak
 
Posts: n/a
Default Re: How to find nested dupes


"Pedro Graca" <hexkid@dodgeit.com> schreef in bericht
news:slrnds5qk6.gck.hexkid@ID-203069.user.individual.net...
> Jaak wrote:
>> There can be dupes in machine - no problem
>> There can be dupes in owner - no problem
>>
>> but I like to find out whether there are machine dupes of the same owner

>
> <snip content="table contents"/>
>
>> I like to do a query to john with result:
>> car
>>
>> If i do the same to brad I get an empty result.

>
> Why don't you check john and brad and whoever else is in the database at
> the same time?
>
>> Can anybody help please me with this?

>
> You want to
>
> *GROUP BY*
>
> the records
>
> *HAVING*
>
> more than 1 machine

No, I want to find the duplicate records where both owner and machine are
the same


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 07:15 AM
Pedro Graca
 
Posts: n/a
Default Re: How to find nested dupes

Jaak wrote:
>
> "Pedro Graca" <hexkid@dodgeit.com> schreef in bericht
> news:slrnds5qk6.gck.hexkid@ID-203069.user.individual.net...
>> Jaak wrote:
>>> There can be dupes in machine - no problem
>>> There can be dupes in owner - no problem
>>>
>>> but I like to find out whether there are machine dupes of the same owner


>> You want to
>>
>> *GROUP BY*
>>
>> the records
>>
>> *HAVING*
>>
>> more than 1 machine

> No, I want to find the duplicate records where both owner and machine are
> the same


Hmmm ... ignore my english.
Is the query giving you the results you wanted?

Try this for effect:

SELECT owner, machine, count(machine)
FROM foo
GROUP BY owner, machine -- HAVING count(machine) > 1
-- WHERE owner = 'john'
;

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 07:17 AM
Andy Hassall
 
Posts: n/a
Default Re: How to find nested dupes

On Mon, 9 Jan 2006 22:57:22 +0100, "Jaak" <jaak@sjakie.jacques> wrote:

>I have a query problem.
>
>There can be dupes in machine - no problem
>There can be dupes in owner - no problem
>
>but I like to find out whether there are machine dupes of the same owner
>
>I like to do a query to john with result:
>car
>
>If i do the same to brad I get an empty result.


mysql> select * from jaak;
+----------+---------+
| owner | machine |
+----------+---------+
| michael | car |
| brad | car |
| jeniffer | laptop |
| john | car |
| john | car |
| john | laptop |
| brad | laptop |
+----------+---------+
7 rows in set (0.00 sec)

mysql> select machine
-> from jaak
-> where owner = 'john'
-> group by machine
-> having count(*) > 1;
+---------+
| machine |
+---------+
| car |
+---------+
1 row in set (0.03 sec)

mysql> select machine
-> from jaak
-> where owner = 'brad'
-> group by machine
-> having count(*) > 1;
Empty set (0.00 sec)

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 07:17 AM
Christopher Pomasl
 
Posts: n/a
Default Re: How to find nested dupes

On Mon, 23 Jan 2006 23:49:28 +0000, Andy Hassall wrote:

>
> mysql> select machine
> -> from jaak
> -> where owner = 'john'
> -> group by machine
> -> having count(*) > 1;
> +---------+
> | machine |
> +---------+
> | car |
> +---------+
> 1 row in set (0.03 sec)
>


And to find all owners with duplicate machines:

mysql> select * from jaak
-> ;
+----------+---------+
| owner | machine |
+----------+---------+
| michael | car |
| brad | car |
| jeniffer | laptop |
| john | car |
| john | car |
| john | laptop |
| brad | laptop |
| brad | laptop |
+----------+---------+
8 rows in set (0.01 sec)
I added aanother laptop for Brad.

mysql> select owner, machine, count(*)
-> from jaak
-> group by owner, machine
-> having count(*) > 1;
+-------+---------+----------+
| owner | machine | count(*) |
+-------+---------+----------+
| brad | laptop | 2 |
| john | car | 2 |
+-------+---------+----------+
2 rows in set (0.13 sec)


Only the duplicate owner/machine are shown for ALL rows with the number of
duplicates.

Chris
Senior Software Engineer, CA
Always remember, you are unique...just like everyone else.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:21 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com