我有以下功能的問(wèn)題:
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'];}
此代碼用于在記分板中顯示組織排名.
因此,例如,當(dāng)我提供組織ID時(shí),我將其排名編號(hào)放在記分板中.
目前的問(wèn)題是我沒(méi)有獲得排名,但我自己將ID稱為組織ID.
例:
get_organization_score(1)
然后我得到排名為1
或者我打電話的時(shí)候
get_organization_score(34)
然后我得到排名為34
但是我希望它在記分牌中的位置由左邊連接來(lái)自其他桌面的XP量排序.
有人可以幫忙嗎?如有任何問(wèn)題請(qǐng)?jiān)儐?wèn)……
編輯:
添加表模式和示例數(shù)據(jù):
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);
當(dāng)我調(diào)用函數(shù)get_organization_score(2);然后組織ID為2,我想查詢表’g_org’,ID為2,LEFT JOIN到表’g_npc’,以獲得’g_npc’表中’g_org_id’值為2的所有條目并且SUM up所有結(jié)果來(lái)自’g_npc’表字段’xp’,然后按字段’xp’DESC對(duì)所有結(jié)果進(jìn)行ORDER BY.
然后,使用@rownum的剩余PHP代碼將使該組織在其他組織中排名.
因此,組織ID 2的以下功能將導(dǎo)致排名為1,因?yàn)樗凇痻p’字段中具有更多XP,并且當(dāng)我將組織ID 1調(diào)用相同的功能時(shí),排名編號(hào)將為2,因?yàn)樗木幪?hào)較小在’XP’領(lǐng)域.
基本上我不希望得到一個(gè)組織清單,但只有排名編號(hào).沒(méi)有其他的.
結(jié)尾應(yīng)該有WHERE org_id =’“.$org_id.”’因?yàn)樗恍枰@得1個(gè)條目的排名.
解決方法:
我唯一能想到的是這個(gè)非常慢的代碼:
select rank from ( select s1.id, @row := @row 1 rank from ( select o.id, sum(coalesce(n.xp, 0) coalesce(n.level, 0) * 128) totalXP from g_org o left join g_npc n on (o.id = n.g_org_id) group by o.id order by totalXP desc ) s1, (select @row := 0) init) s2where id = 1
注意,不需要運(yùn)行SET查詢,并記住用你在PHP中使用的任何東西替換id = 1.
來(lái)源:https://www.icode9.com/content-2-311101.html聯(lián)系客服