MySQL: Total GROUP BY WITH ROLLUP curiosity

后端 未结 3 1556
醉梦人生
醉梦人生 2020-12-31 01:01

I have two queries. One of them makes sense to me, the other don\'t. First one:

SELECT gender AS \'Gender\', count(*) AS \'#\'
    FROM registrations 
    GR         


        
3条回答
  •  既然无缘
    2020-12-31 01:41

    Coz when you use JOIN method, the following NULL element of array will have value of previous NOT NULL element. But Im not sure. Thats my experience when I use it in PHP.

    hm... there is another problem... 'Country' canot be because it is name of table. So change for something else. Then last result will display NULL. Here is my proposal:

    $result = mysql_query("SELECT c.printable_name AS 'countryp', count(*) AS '#'
    FROM registrations r, country c WHERE r.country = c.country_id
    GROUP BY countryp WITH ROLLUP");
    
    while($row = @mysql_fetch_array($result)) {
      $r1 = $row["countryp"];
      $r2 = $row["#"];
      if ($r1 == NULL) $r1 = 'Total';
      echo "$r1 $r2
    "; }

提交回复
热议问题