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
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 0 of 0 people found this answer helpful. Did you? Yes No 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.
If you want to count then may be you can try this..
SELECT DISTINCT visited_id,COUNT(visited_id) AS visit_num FROM profile_hitsor 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 1 of 1 people found this answer helpful. Did you? Yes No |
© Advanced Web Core. All rights reserved