I have a table with ~100,000 rows.
I am trying to print to screen the most frequently used “type_num” from a column.
So if 2362 appears 1000 times in the type_num column, that being the most used type, and 1234 is used 987 times and so on, I get a desc list of most freq used type_num‘s.
No luck. Here is my newbie attempt:
my $mostused = DBI->connect("$thedb","$user","$password") or die "Connection Error: $DBI::errstr\n";
my $getfreq = $mostused->prepare(qq{SELECT `type_num` count(*) FROM `productstable` GROUP BY `type_num` ORDER BY count(*) DESC LIMIT 10}); ##just tested with 10
$getfreq->execute() or die "Connection Error: $DBI::errstr\n";
while(my ($type_num) = $getfreq->fetchrow_array()) {
print qq~$type_num<br />~;
}
$getfreq->finish(); #not sure if correct
$mostused->disconnect; #not sure if correct
Connection Error: You have an error in
your SQL syntax; check the manual that
corresponds to your MySQL server
version for the right syntax to use
near ‘count(*) FROM `productstable`
GROUP BY `type_id` ORDER BY count(*)
DESC LIMIT 10’ at line 1
The type_num’s are a 1 to 5 digit number. (Using DBI and strict but this example is cleaned up.)
I have tried several variations and examples I found while searching here but, I am doing something wrong. please help.
Change
To
Comma after type_num + alias for
count(*)to reuse in theORDER BY.