grandadevans Posted June 6, 2006 Share Posted June 6, 2006 Hello everybody yet again,I have a table which logsA) The users IP addressB) The company ID of the company clickedThe table looks like this[code]+-----------+--------------+------+-----+-------------------+----------------+| Field | Type | Null | Key | Default | Extra |+-----------+--------------+------+-----+-------------------+----------------+| clickID | int(11) | NO | PRI | NULL | auto_increment || date | timestamp | YES | MUL | CURRENT_TIMESTAMP | || companyID | int(11) | YES | MUL | NULL | || URL | varchar(255) | YES | | NULL | || referer | varchar(50) | YES | MUL | NULL | || IPAddress | varchar(100) | YES | | NULL | || agent | varchar(255) | NO | MUL | | |+-----------+--------------+------+-----+-------------------+----------------+[/code]How do I select instances of the same IP address with the same compay IDfor example, what I want to do isDELETE rows where the same IP address and companyID combination appear more than 4 times.Can anybody help me out?ThanksJohn Quote Link to comment https://forums.phpfreaks.com/topic/11308-selecting-mutiple-rows-with-same-values/ Share on other sites More sharing options...
nickk Posted June 6, 2006 Share Posted June 6, 2006 Hey I'm in a hurry so I have to be quick. Basically, to delete rows where the same IP address and companyID combination appear more than 4 times, you could select the first row, then loop through all the other rows, comparing the first row to the ones that are looping. Using a counter, you could count the number of times the first row is equal to the others (in terms of IP address and companyID), if the counter is 4=< you delete it. Then you move onto the second row and do the same thing. This is a very rough idea and you could make it much more effecient, maybe there is also a simple mysql statement that could be of more use (I'm not that great at mysql to know it). Hope I helped. Quote Link to comment https://forums.phpfreaks.com/topic/11308-selecting-mutiple-rows-with-same-values/#findComment-42355 Share on other sites More sharing options...
fenway Posted June 6, 2006 Share Posted June 6, 2006 Once you confirm that the following query find all these rows:[code]SELECT companyID, IPAddress, COUNT(*) AS cnt FROM yourTable GROUP BY companyID, IPAddress HAVING cnt >= 4[/code]You can easily switch this to a DELETE statement.BTW, you should be storing your IPs as UNSIGNED INTs and using INET_ATOI(). Quote Link to comment https://forums.phpfreaks.com/topic/11308-selecting-mutiple-rows-with-same-values/#findComment-42487 Share on other sites More sharing options...
grandadevans Posted June 7, 2006 Author Share Posted June 7, 2006 Cheers mate,I've also implemented the suggestion about storing IP addresses.John Quote Link to comment https://forums.phpfreaks.com/topic/11308-selecting-mutiple-rows-with-same-values/#findComment-42684 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.