[PHP-DB] MySQL query question
i want to search the table called comments, and i want to count the number of times artid appears. then i want to group them all together, but then i want to order them by most appearances to fewest. so if artid 1 appeared 40 times and it was the most, i want that to be retrieved first. if artid 3 appeared 38 times and it was second most, i want that retrieved second... etc and so on. this code ive given is wrong, but i know that it has to be something like this or that im close. if any of you could help me id really appreciate it. if theres a shortcut with this code that would be great, otherwise i have to run my php around in a ton of different mysql queries trying to find out which has the most and have it ordered from highest to lowest. thank you for your help. ive tried mysql.com too and i cant find a thing there. plus the mailing lists for mysql, no one ever responds! thank you php list, you are my only hope. ;P -james $mostcomquery = SELECT artid, COUNT(*) FROM comments GROUP BY artid ORDER BY (COUNT(*)) DESC LIMIT 5;
[PHP-DB] MySQL Query Question
Hello, I need some help from you MySQL gurus out there. This is pure SQL by the way..no front-end. Lets say you have a table that has 2columns.columnA, ColumnB and columnC I have something like this if (columnA'7', (columnB/columnC) + 52, columnB/ColumnC) as Number however i need to test if columnc is 0 also cause if i need divide by zero i get a null value I cant figure out how to do this any help would be greatly appreciated -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] MySQL Query Question
Jeremy, This question probably should be posted to the MySql mailing list but here goes. Take a look at this link: http://www.mysql.com/doc/en/Where_optimisations.html Your example was not very explicit so I am not sure, but I believe you are in need of a WHERE clause. For example; Select a, b, c where a 7 and c 0 This should get you going in the right direction. At 03:29 PM 1/23/03 -0600, Jeremy wrote: Hello, I need some help from you MySQL gurus out there. This is pure SQL by the way..no front-end. Lets say you have a table that has 2columns.columnA, ColumnB and columnC I have something like this if (columnA'7', (columnB/columnC) + 52, columnB/ColumnC) as Number however i need to test if columnc is 0 also cause if i need divide by zero i get a null value I cant figure out how to do this any help would be greatly appreciated -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] MySQL Query Question
It is safe to test if the divisor is zero: replace columnB/columnC by IF( columnC = 0, 0, columnB/columnC ) (the 0 could be anything else; this depends on your business logic) Ignatius - Original Message - From: Jeremy [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, January 23, 2003 10:29 PM Subject: [PHP-DB] MySQL Query Question Hello, I need some help from you MySQL gurus out there. This is pure SQL by the way..no front-end. Lets say you have a table that has 2columns.columnA, ColumnB and columnC I have something like this if (columnA'7', (columnB/columnC) + 52, columnB/ColumnC) as Number however i need to test if columnc is 0 also cause if i need divide by zero i get a null value I cant figure out how to do this any help would be greatly appreciated -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] MySQL Query Question
I need some help from you MySQL gurus out there. This is pure SQL by the way..no front-end. Lets say you have a table that has 2columns.columnA, ColumnB and columnC I have something like this Oh CRAP! You lost me... you said two columns but listed 3! ;) if (columnA'7', (columnB/columnC) + 52, columnB/ColumnC) as Number I think someone already suggested these, but either include a WHERE c != 0 in your WHERE clause (if you don't want those rows), or use a nested IF() to return a specific value when C is zero: IF(ColumnC=0,'Undef',IF(ColumnA7,ColumnB/ColumnC+52,ColumnB/ColumnC)) ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php