Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 3rd, 2007, 9:12 AM   #1
davil
Newbie
 
Join Date: Nov 2007
Posts: 26
Rep Power: 0 davil is on a distinguished road
Select Non distinct in MySQL

Hi all,

I know this isn't exactly PHP but I was wondering if there's a simple MySQL query you can use to get back non-distinct lines... like for example you can use SELECT DISTINCT to only give you back the rows with that unique value in a field or whatever... but I'm looking to do the opposite.. I can filter the data with PHP if needs be but I'd much prefer to only get the correct data from the SQL instead and leave unneeded programming out of it...

I have a field called mac address on my DB and there are duplicate items and I simply want to be able to pull up details for the mac addresses that are reported twice so I can remove old data from the DB....

for example, if I wanted to get back unique mac addresses on the DB only I would do this:

php Syntax (Toggle Plain Text)
  1. $sql = "SELECT DISTINCT `primarymac` FROM `aidahardware` ORDER BY 'datestamp' DESC";
  2. $result = mysql_query($sql) or die(mysql_error());

but how do I do the exact opposite??

Any help is much appreciated as I cannot find the answer I'm looking for through google etc.
davil is offline   Reply With Quote
Old Dec 3rd, 2007, 9:14 AM   #2
davil
Newbie
 
Join Date: Nov 2007
Posts: 26
Rep Power: 0 davil is on a distinguished road
Re: Select Non distinct in MySQL

I tell a lie, I've just found what I think I'm looking for :

SELECT primarymac, COUNT(*) as count
FROM aidahardware
GROUP BY primarymac
HAVING count > 1
davil is offline   Reply With Quote
Old Dec 3rd, 2007, 11:10 AM   #3
Arla
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 143
Rep Power: 4 Arla is on a distinguished road
Re: Select Non distinct in MySQL

Was going to recommend exactly that.

Having Count > X is a useful query.
Arla is offline   Reply With Quote
Old Dec 3rd, 2007, 11:33 AM   #4
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 499
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Select Non distinct in MySQL

Don't know about MySql but Sybase database the having clause can be somewhat slow, so I would code with the where clause

SELECT primarymac, COUNT(*) as count
FROM aidahardware
where count > 1
Ancient Dragon is offline   Reply With Quote
Old Dec 4th, 2007, 1:55 AM   #5
Sanjay Aggarwal
Newbie
 
Join Date: Oct 2007
Posts: 12
Rep Power: 0 Sanjay Aggarwal is an unknown quantity at this point
Re: Select Non distinct in MySQL

SELECT primarymac, COUNT(*) as count
FROM aidahardware
GROUP BY primarymac
HAVING count > 1

Sanjay Aggarwal
Sanjay Aggarwal is offline   Reply With Quote
Old Dec 4th, 2007, 2:03 AM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Select Non distinct in MySQL

Once again, Sanjay Aggarwal manages to regurgitate information identical to what has already been posted in a thread.
titaniumdecoy is offline   Reply With Quote
Old Dec 4th, 2007, 3:49 AM   #7
davil
Newbie
 
Join Date: Nov 2007
Posts: 26
Rep Power: 0 davil is on a distinguished road
Re: Select Non distinct in MySQL

Quote:
Originally Posted by Ancient Dragon View Post
Don't know about MySql but Sybase database the having clause can be somewhat slow, so I would code with the where clause

SELECT primarymac, COUNT(*) as count
FROM aidahardware
where count > 1
I tried that and got the following error:
Unknown column 'count' in 'where clause'
davil is offline   Reply With Quote
Old Dec 4th, 2007, 3:53 AM   #8
davil
Newbie
 
Join Date: Nov 2007
Posts: 26
Rep Power: 0 davil is on a distinguished road
Re: Select Non distinct in MySQL

Ok so my original code looked right except I wanted everything so this is what worked for me:

sql Syntax (Toggle Plain Text)
  1. SELECT *, COUNT(*) AS count FROM aidahardware GROUP BY primarymac HAVING count > 1
davil is offline   Reply With Quote
Old Dec 4th, 2007, 10:06 AM   #9
davil
Newbie
 
Join Date: Nov 2007
Posts: 26
Rep Power: 0 davil is on a distinguished road
Re: Select Non distinct in MySQL

Well I have this code working great now except for one thing... I can't get a number of how many results coming back from the DB and store it to a variable. see my variable $total_results... what am I doing wrong?
Error message is
supplied argument is not a valid MySQL result resource in non_unique_mac.php


php Syntax (Toggle Plain Text)
  1. <?php
  2. require('config.php');
  3.  
  4. $sql = "SELECT *, COUNT(*) as count FROM `aidahardware` WHERE `primarymac` not like '00-00-00-00-00-00' GROUP BY primarymac HAVING count > 1 ";
  5. $sqltwo = "SELECT COUNT(*) as Num FROM `aidahardware` WHERE `primarymac` not like '00-00-00-00-00-00' GROUP BY primarymac HAVING count > 1 ";
  6.  
  7. $total_results = mysql_result(mysql_query($sqltwo),0);
  8.  
  9. $sqlcake = mysql_query($sql) or die(mysql_error());
  10.  
  11.  
  12. echo "TOTAL RESULTS=$total_results<br>";
  13.  
  14.  
  15. echo "<br><br>";
  16. echo "<table border='1'>";
  17.  
  18. echo "<tr>\n";
  19. echo "<td><b>Primary Mac</a></td>";
  20.  
  21. while($row = mysql_fetch_array($sqlcake)){
  22. echo "</tr><tr>\n";
  23. echo "<td><b><a href='index.php?locate=showmac&mac=$primarymac'>$primarymac</td>";
  24. }
  25.  
  26. echo "</tr>";
  27. echo "</table>\n";
  28.  
  29. ?>
davil is offline   Reply With Quote
Old Dec 4th, 2007, 11:42 AM   #10
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Select Non distinct in MySQL

Wow. Slow down. Use int mysql_num_rows ( resource $result ) if you want to get the number of results for a query.
Sane is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Connecting MySQL and PHP titaniumdecoy PHP 10 Feb 25th, 2008 7:47 PM
MySql paulchwd Other Web Development Languages 8 Feb 8th, 2007 9:17 PM
Better way to Select from MySQL? Xeoncross PHP 3 Oct 3rd, 2006 3:48 PM
SQL select distinct OpenLoop Other Programming Languages 2 Apr 10th, 2006 8:30 PM
Tutorial - Using MySQL in C# Darkhack C# 12 Jan 17th, 2006 9:28 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC