MySQL: LEFT JOIN in a php function with @rownum?

I have a problem with the following function:

function get_organization_score($org_id)
{
  $sql1 = "SET @rownum := 0";
  $sql2 = "SELECT rank, xp_total FROM (
           SELECT @rownum := @rownum + 1 AS rank, g_org.id AS org_id,
           (Sum(g_npc.xp) + (Sum(g_npc.level)*128)) AS xp_total FROM g_org
           LEFT JOIN g_npc ON g_org.id = g_npc.g_org_id GROUP BY g_org.id
           ORDER BY xp_total DESC
                          ) as result WHERE org_id='".$org_id."'";
  mysql_query($sql1);
  $result = mysql_query($sql2);
  $rows = '';
  $data = array();
  if (!empty($result))
      $rows = mysql_num_rows($result);
  else
      $rows = '';
  if (!empty($rows)){
      while ($rows = mysql_fetch_assoc($result)){
          $data[] = $rows;
      }
  }
  if (empty($data[0]['rank']))
      return 1;
  return $data[0]['rank'];
}

This code is to show a organization rank in a scoreboard.
So for example when I give organization ID then I get its rank number positioned in the scoreboard.

Currently the problem is that I don’t get the rank number but the ID itself that I am calling the function as organization id.

Example:
get_organization_score(1)
then I get rank number as 1
or when I call
get_organization_score(34)
then I get rank number as 34

but I want its position in the scoreboard ordered by the XP amount that comes from other table by left join.

Can anyone help? If any questions please ask…

EDIT:
Adding table schema and sample data:

CREATE TABLE IF NOT EXISTS `g_org` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `org_name` varchar(255) NOT NULL,
  `founded` datetime NOT NULL,
  `g_userid` int(255) NOT NULL,
  `g_username` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `g_userid` (`g_userid`),
  KEY `g_username` (`g_username`(191)),
  KEY `g_username_2` (`g_username`),
  KEY `org_name` (`org_name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=47 ;

INSERT INTO `g_org` (`id`, `org_name`, `founded`, `g_userid`, `g_username`) VALUES
(1, 'test_org_1', '2012-07-04 02:30:56', 1, 'DDD'),
(2, 'test_org_2', '2012-07-04 02:34:57', 2, '777');

--
-- Table structure for table `g_npc`
--

CREATE TABLE IF NOT EXISTS `g_npc` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `g_userid` int(255) NOT NULL,
  `g_username` varchar(255) NOT NULL,
  `g_org_id` int(255) NOT NULL,
  `g_org_name` varchar(255) NOT NULL,
  `g_npc_name` varchar(255) NOT NULL,
  `level` int(255) NOT NULL,
  `xp` int(255) NOT NULL,
  `last_update` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `g_userid` (`g_userid`),
  KEY `g_username` (`g_username`),
  KEY `g_org_id` (`g_org_id`),
  KEY `g_org_name` (`g_org_name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=181 ;


INSERT INTO `g_npc` (`id`, `g_userid`, `g_username`, `g_org_id`, `g_org_name`, `g_npc_name`, `level`, `xp`, `last_update`) VALUES
(1, 1, 'DDD', 1, 'test_org_1', 'Kinuzehute Doqasi', 4, 155, '2012-11-13 14:30:54'),
(2, 1, 'DDD', 1, 'test_org_1', 'Zabava Qigatagise', 2, 107, '2012-10-30 17:38:12'),
(3, 2, '777', 2, 'test_org_2', 'Roci Vuzoducu', 6, 135, '2012-11-13 23:17:40'),
(4, 2, '777', 2, 'test_org_2', 'Gexoye Zeze', 4, 638, '2012-11-06 02:02:27'),
(5, 2, '777', 2, 'test_org_2', 'Sibalabu Gozi', 9, 285, '2012-11-06 02:02);

When I call a function get_organization_score(2); then the organization id would be 2 and I would like to make a query to table ‘g_org’ with ID as 2 and LEFT JOIN to table ‘g_npc’ to get all entries with ‘g_org_id’ value 2 in ‘g_npc’ table and SUM up all results from ‘g_npc’ table field ‘xp’ and then ORDER BY all those results by field ‘xp’ DESC.

Then the remaining PHP code with @rownum would give this organization a ranking among the other organizations.

So the following function by organization ID 2 would result the ranking to be 1 because it has more XP in the ‘xp’ field and when I would call the same function with organization ID 1 then the ranking number would be 2 because it has smaller number in the ‘XP’ field.

Basicly I dont want to result a list of organization but only the ranking number. Nothing else.

The ending should have WHERE org_id=’”.$org_id.”‘ because it needs to get the ranking number for only 1 entry.

Leave a Reply

*

Hire Me
Follow Me!
Search
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.
Categories