乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      MySQL:在@rownum的php函數(shù)中LEFT JOIN?

       印度阿三17 2019-07-09

      我有以下功能的問題:

      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'];
      }
      

      此代碼用于在記分板中顯示組織排名.
      因此,例如,當我提供組織ID時,我將其排名編號放在記分板中.

      目前的問題是我沒有獲得排名,但我自己將ID稱為組織ID.

      例:
      get_organization_score(1)
      然后我得到排名為1
      或者我打電話的時候
      get_organization_score(34)
      然后我得到排名為34

      但是我希望它在記分牌中的位置由左邊連接來自其他桌面的XP量排序.

      有人可以幫忙嗎?如有任何問題請詢問……

      編輯:
      添加表模式和示例數(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);
      

      當我調(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é)果來自’g_npc’表字段’xp’,然后按字段’xp’DESC對所有結(jié)果進行ORDER BY.

      然后,使用@rownum的剩余PHP代碼將使該組織在其他組織中排名.

      因此,組織ID 2的以下功能將導致排名為1,因為它在’xp’字段中具有更多XP,并且當我將組織ID 1調(diào)用相同的功能時,排名編號將為2,因為它的編號較小在’XP’領(lǐng)域.

      基本上我不希望得到一個組織清單,但只有排名編號.沒有其他的.

      結(jié)尾應該有WHERE org_id =’“.$org_id.”’因為它只需要獲得1個條目的排名.

      解決方法:

      我唯一能想到的是這個非常慢的代碼:

      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
      ) s2
      where id = 1
      

      注意,不需要運行SET查詢,并記住用你在PHP中使用的任何東西替換id = 1.

      來源:https://www./content-2-311101.html

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多