0

I have a table (url_log) with a column (referer) of urls. Some urls are unique and some are duplicates. Using PHP I want to display each different url (without repeating) and the number of times the url appears in the column.

This is what I came up with but it is definitely wrong:

echo '<table>';

$ref=$icdb->get_row("SELECT COUNT(referer) AS frequency, referer FROM url_log WHERE u = '".$dom."' GROUP BY referer ORDER BY frequency DESC");

foreach ($ref as $details) {
echo "<tr><td>".$details['referer']."</td><td>".$details['frequency']."</td></tr>";
}

echo '</table>';

Any tips?

2
  • Why PHP? Use MySQLs GROUP BY and DISTINCT. Much faster and less code. Commented Feb 10, 2014 at 8:14
  • can you add some sample data and desired output ? Commented Feb 10, 2014 at 8:20

2 Answers 2

2

If I understand your question, this should do it.

SELECT DISTINCT(referer), COUNT(referer) AS frequency
FROM url_log 
GROUP BY referer
ORDER BY frequency DESC;
Sign up to request clarification or add additional context in comments.

3 Comments

I used: $result= mysql_query("SELECT DISTINCT(referer), COUNT(referer) AS frequency FROM url_log WHERE u = '".$page."' GROUP BY referer ORDER BY frequency DESC"); while ($details=mysql_fetch_array($result)) { echo "<tr><td>".$details['referer']."</td><td>".$details['COUNT(referer)']."</td></tr>"; } But I can't seem to display the count
Change: $details['COUNT(referer)'] to $details['frequency']
No problem, sometimes just needs a second pair of eyes on it. You're welcome to up my comment too if it was helpful. :)
1

for selecting each different value of a column use this query

SELECT DISTINCT(column) AS columnname_key FROM table ;

to count duplicate use this code

SELECT Count(*) duplicatetable
FROM
(
    select columname_to_check_dulicate_values, Count(*)
    from tablename
    group by name
    having Count(*) > 1
) x

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.