0

I have the following tableCountry

country  clicks
-------  ------
0        222
66       34 
175      1000
45       650

And I use the following MYSQL statement to get the ranking of any of the country based on the clicks column (just one result)

SELECT COUNT(*) rank 
FROM countryTable a 
JOIN countryTable b 
   ON a.clicks <= b.clicks 
WHERE a.country = 45

The above will return '2'. Then in my php code I try to access the rank value with

$row =  mysql_fetch_array($result) or die(mysql_error()); 
echo $row['rank'];

But this doesn't return any result if the country is the number one. i.e a.country = 175

Taryn
  • 242,637
  • 56
  • 362
  • 405
PutraKg
  • 2,226
  • 3
  • 31
  • 60

3 Answers3

1

A join ON is a join between columns, not a comparison.

UPDATED

SELECT COUNT(*)+1 rank 
FROM countryTable
WHERE clicks > (SELECT clicks FROM countryTable WHERE country = 45)

Reasoning: searching for a rank mean searching for the number of records that has clicks > a given click.

Therefore, for 175 there is 0 country with better click => rank 1, country 45, 1 country with better click => rank 2

PHP

$result = mysql_query("....")
$row = mysql_fetch_array($result)
...

Normally it should work unless you got a problem with connecting to the server. That's where you should use your debugging skill. Do a var_dump($result) to see if it return false, if yes then it's a connection problem (check mysql_connect or something)

Thanh Trung
  • 3,566
  • 3
  • 31
  • 42
  • Thanks. Just tried this but it didn't return the correct ranking. Getting the first ranking still returns the blank result also – PutraKg Jan 12 '13 at 17:16
  • Error: Unknown column 'a.country' in 'where clause'. BTW I have tried several mysql statement which are all correctly returning values in MYSQL. But in PHP all returns nothing for the number one country. – PutraKg Jan 12 '13 at 17:45
  • It's fixed, try this new sql – Thanh Trung Jan 12 '13 at 17:52
  • The new sql is correct. But how do I get $row['rank'] = 1 for the the number one country? I still get a blank result in php (not mysql). – PutraKg Jan 12 '13 at 17:57
  • That's what I am using and strangely it doesn't return any value only when the country is number one in the ranking. It's not a connection problem for sure because the problem is specific to that condition. I am not a PHP or MYSQL programmer so I can't go far with my own debugging :( – PutraKg Jan 12 '13 at 18:13
  • I tested var_dump($result) but it doesn't return any value when it's the number one ranking country. How can this be a connection problem when other queries is returning the correct result? – PutraKg Jan 12 '13 at 18:37
  • Then again my friend, I have no idea. $result should always contain a resource of "false". If it doesn't show up any anything, it means that your code is broken somewhere, maybe above it. Like mysql_query is not executing – Thanh Trung Jan 12 '13 at 18:42
1

If you're looking for ranking, use this:

SELECT @rownum := @rownum + 1 AS num,
    t.country, t.clicks
FROM countryTable t, 
    (SELECT @rownum := 0) r
ORDER BY t.clicks DESC

Result

| NUM | COUNTRY | CLICKS |
--------------------------
|   1 |     175 |   1000 |
|   2 |      45 |    650 |
|   3 |       0 |    222 |
|   4 |      66 |     34 |

See it in action

Kermit
  • 33,827
  • 13
  • 85
  • 121
  • Thanks. In my case though I just want the query to return one result. The problem I have is accessing the return value in PHP when the country ranked 1. – PutraKg Jan 12 '13 at 17:49
  • Interesting, this way you can have a list of rank when you need it. You can simply get a record with WHERE country=id_country – Thanh Trung Jan 12 '13 at 17:55
0

@PutraKg I think you can close this question, because I answered it in this post MYSQL does not return result in PHP when asked for the first ranking only ;-)

Community
  • 1
  • 1
Mr. Radical
  • 1,847
  • 1
  • 19
  • 29