PHP MYSQL 临时表的使用

时间:2023-12-13 10:57:44
/**
* 临时表:用于获取爱鸽登录分类数量
*/
$temporaryTableName = uniqid('temporary_');
$model = M();
$model->execute("CREATE TEMPORARY TABLE `{$temporaryTableName}` (
`id` int unsigned,
`leixing` tinyint(3) unsigned,
`dangqianzhuangtai` tinyint(3) unsigned
);");
/**
* dangqianzhuangtai
* 1 - 存棚
* 2 - 转让
* 3 - 丢失
* 4 - 死亡
* 5 - 外借
*/
$model->execute("INSERT INTO `{$temporaryTableName}`(`id`,`leixing`,`dangqianzhuangtai`) SELECT `id`,`leixing`,IF(`dangqianzhuangtai` IS NULL OR `dangqianzhuangtai` = '','0',`dangqianzhuangtai` ) FROM `mis_auto_hxwyg` WHERE `yonghu` = '1447'"); // return $model->query("SELECT * FROM `{$temporaryTableName}`");
$data['num1'] = $model->query("SELECT * FROM (SELECT COUNT(*) AS `count` FROM `{$temporaryTableName}` WHERE `leixing` = '1' and `dangqianzhuangtai` = '1') AS `b`");
$data['num2'] = $model->query("SELECT COUNT(*) AS `count` FROM `{$temporaryTableName}` WHERE `leixing` = '1' and `dangqianzhuangtai` = '2'");
$data['num3'] = $model->query("SELECT COUNT(*) AS `count` FROM `{$temporaryTableName}` WHERE `leixing` = '1' and `dangqianzhuangtai` = '3'");
$data['num4'] = $model->query("SELECT COUNT(*) AS `count` FROM `{$temporaryTableName}` WHERE `leixing` = '1' and `dangqianzhuangtai` = '4'");
$data['num5'] = $model->query("SELECT COUNT(*) AS `count` FROM `{$temporaryTableName}` WHERE `leixing` = '1' and `dangqianzhuangtai` = '5'"); $data['num6'] = $model->query("SELECT COUNT(*) AS `count` FROM `{$temporaryTableName}` WHERE `leixing` = '2' and `dangqianzhuangtai` = '1'");
$data['num7'] = $model->query("SELECT COUNT(*) AS `count` FROM `{$temporaryTableName}` WHERE `leixing` = '2' and `dangqianzhuangtai` = '2'");
$data['num8'] = $model->query("SELECT COUNT(*) AS `count` FROM `{$temporaryTableName}` WHERE `leixing` = '2' and `dangqianzhuangtai` = '3'");
$data['num9'] = $model->query("SELECT COUNT(*) AS `count` FROM `{$temporaryTableName}` WHERE `leixing` = '2' and `dangqianzhuangtai` = '4'");
$data['num10'] = $model->query("SELECT COUNT(*) AS `count` FROM `{$temporaryTableName}` WHERE `leixing` = '2' and `dangqianzhuangtai` = '5'");
return $data;