Have an account? Sign in
Login  Register  Facebook
handling duplicates in mysql
Hi All!

please, I need some help to get started with a script for finding and handling duplicate rows in a mysql table.
Here's the table structure :
Code:

+------+---------------+----------+-------------+----------+
| id | IP address | user | visited_id | date |
+------+---------------+----------+-------------+----------+
| 1 | 123.1.2.3 | user_1 | 10 | 05.05.10 |
+------+---------------+----------+-------------+----------+
| 2 | 123.2.3.4 | user_2 | 12 | 05.05.10 |
+------+---------------+----------+-------------+----------+
| 3 | 123.1.2.3 | user_1 | 10 | 06.06.10 |
+------+---------------+----------+-------------+----------+

Now I need to list ALL the rows where the "visited id" is the logged user's ID :

$q_hits = mysql_query("SELECT * FROM profile_hits WHERE visited_id = '$user_ID'"); 
As you can see, sometimes ONE unique visitor (column user) is inserted more than once, depending on the date he/she visited a page. I'd like to list ALL the visits to the page with ID "visited_id" BUT to avoid echoing the same user more than once. Is it possible to create an array within the
while ($hits_rows = mysql_fetch_array($q_hits)) 


loop and then to increment a variable if the user is the same (take the last visit date)?
Any help/idea is highly appreciated!
Regards
Started: September 18, 2011 Latest Activity: September 18, 2011 mysql php duplicate
2 Answers
do you know you can make each use have just one row in the table
by the php you can check if there is an row if there is one you can update it and if there is no you can insert new one

Posted: xtremex
In: September 18, 2011

I agree, i think its better idea to check before inserting in database. if the certain user_id or ip-address exists, then update the row count, if it does not exist then insert the row.
September 18, 2011

If you want to count then may be you can try this..

SELECT DISTINCT visited_id,COUNT(visited_id) AS visit_num FROM profile_hits
or
SELECT visited_id, COUNT(*) AS visit_num FROM profile_hits GROUP BY id


Im not an expert, so hopefully jooria, is_set or go can answer. So please wait.

Posted: mac
In: September 18, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer