sql查询选择和状态明智

时间:2014-10-10 12:17:08

标签: php mysql

id  | partyname | state     | constituency  | candidatename |    district  |    votes
----------------------------------------------------------------------------------
82  BJP         Maharashtra  Nagpur-East    Nitin Gadkari     Nagpur            1200

83  AAP         Maharashtra  Nagpur-East    Arvind Kejriwal   Nagpur            750

84  BJP         Maharashtra  Nagpur-West    Vicky             Nagpur            4800

86  AAP         Maharashtra  Nagpur-West    Lucky             Nagpur            3500

87  BJP         Maharashtra  Nagpur-North   Rakesh            Nagpur            100

88  AAP         Maharashtra  Nagpur-North   Ravan             Nagpur            1500

89  BJP         Maharashtra  Amravati-East  Amr               Amravati          320

下面是我的查询,以获取最大值和partyname的总和。 它完美无缺,但它只选择了nagpur consittuenciy

计算不选择amravati的数量。

上面的表格显示了我在表格中的值... 查询显示

aap = 1

bjp = 2

但显示bjp = 3 bcoz这个bjp也是amravati-east的最大价值。

我的意思是查询工作完美但这选择马哈拉施特拉邦的所有数据... PLZ建议我......

 <?php                
        $sqltoatl = "select t1.partyname as PARTY, count(*) as WinCount from voter_count t1, voter_count t2 where t1.votes > t2.votes and t1.constituency=t2.constituency
        group by t1.partyname";
        $resulttoatl = mysql_query($sqltoatl);
        WHILE($rowtoatl = mysql_fetch_array($resulttoatl,MYSQL_ASSOC))
        {
        ?>                
         <tr>  

          <td><?php echo $rowtoatl['PARTY']; ?></td>
          <td><?php echo $rowtoatl['WinCount']; ?></td>
          </tr>
        </table>
</td>
    <?php } ?>

1 个答案:

答案 0 :(得分:0)

试试这个。不确定你是否在寻找这样的东西。

  CREATE TABLE IF NOT EXISTS voter_count AS
      ( SELECT *
       FROM
         ( SELECT 82 AS id,
                        'BJP' partyname,
                        'Maharashtra' STATE,
                        'Nagpur-East' AS constituency ,
                        'Nitin Gadkari' candidatename,
                        'Nagpur' district,
                        1200 votes
          UNION SELECT 83,
                       'AAP',
                       'Maharashtra' ,
                       'Nagpur-East' ,
                       'Arvind Kejriwal',
                       'Nagpur',
                       750
          UNION SELECT 84,
                       'BJP',
                       'Maharashtra' ,
                       'Nagpur-West' ,
                       'Vicky',
                       'Nagpur',
                       4800
          UNION SELECT 86,
                       'AAP',
                       'Maharashtra' ,
                       'Nagpur-West' ,
                       'Lucky',
                       'Nagpur',
                       3500
          UNION SELECT 87,
                       'BJP',
                       'Maharashtra' ,
                       'Nagpur-North' ,
                       'Rakesh',
                       'Nagpur',
                       100
          UNION SELECT 88,
                       'AAP' ,
                       'Maharashtra' ,
                       'Nagpur-North' ,
                       'Ravan',
                       'Nagpur',
                       1500
          UNION SELECT 89,
                       'BJP',
                       'Maharashtra',
                       'Amravati-East' ,
                       'Amr',
                       'Amravati',
                       320) VoteTable );

此查询将为您提供完整的详细信息,这在有平局时非常有用。

    SELECT voter_count.*
    FROM
      (SELECT constituency,
              MAX(votes) Votes
       FROM voter_count
       GROUP BY constituency) results
    INNER JOIN voter_count ON voter_count.Votes = results.Votes
    AND voter_count.constituency = results.constituency;

可能是您正在寻找的查询

    SELECT partyname,
           count(*)
    FROM
      (SELECT constituency,
              MAX(votes) Votes
       FROM voter_count
       GROUP BY constituency) results
    INNER JOIN voter_count ON voter_count.Votes = results.Votes
    AND voter_count.constituency = results.constituency
    GROUP BY partyname