SQL之按两个字段分类汇总

时间:2022-05-24 15:08:04

目的:

同时按“游戏代号”和“礼包名”分类汇总,然后获取下拉框的数据。

 如下图所示:

SQL之按两个字段分类汇总

 

SQL查询

select game,giftname from qyg_gifts group by game , giftname 

 

//连接mysql数据库
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("connect failed" . mysql_error());
mysql_select_db(DB_DATABASENAME, $conn);

$existing_gift= []; //现存的礼包
$sql="select game,giftname from qyg_gifts group by game,giftname ";
$query=mysql_query($sql);
while($row = mysql_fetch_array($query)){
array_push($existing_gift, [$row[game],$row[giftname] ]);
}
mysql_close($conn);