PHP SQL in 1查询

PHP SQL in 1查询,php,mysql,Php,Mysql,对不起,简单的问候。我是Sql新手。所以,我有一个sql查询的耦合,我想在1个查询中编写这个sql $sql1=select 'max(id) as max1 FROM table where status_id=1 LIMIT 1'; $sql2=select 'max(id) as max2 FROM table where status_id=2 LIMIT 1'; $sql3=select 'max(id) as max3 FROM table where status_id=3 LIM

对不起,简单的问候。我是Sql新手。所以,我有一个sql查询的耦合,我想在1个查询中编写这个sql

$sql1=select 'max(id) as max1 FROM table where status_id=1 LIMIT 1';
$sql2=select 'max(id) as max2 FROM table where status_id=2 LIMIT 1';
$sql3=select 'max(id) as max3 FROM table where status_id=3 LIMIT 1';
$sql4=select 'max(id) as max4 FROM table where status_id=4 LIMIT 1';

$query1=mysql_query($sql1);
$fetch1=mysql_fetch_array($query1);
$query2=mysql_query($sql2);
$fetch2=mysql_fetch_array($query2);
$query3=mysql_query($sql3);
$fetch3=mysql_fetch_array($query3);
$query4=mysql_query($sql4);
$fetch4=mysql_fetch_array($query4);

echo 'Max ID numbers are:'.$fetch1['max1'].'; '.$fetch2['max2'].'; '.$fetch3['max3'].'; '.$fetch4['max4'].'';
如何在一个查询中编写这4个sql并获取合适的ID?我尝试了MYSQL UNION,但没有任何结果。

使用此查询

SELECT `status_id`, 
       max(`id`) as `max` 
FROM table 
GROUP BY `status_id`
这将为您提供所有成对的
status\u id
和相应的
max(id)
per
status\u id
。然后,您可以在常规(
while
)循环中获取结果

标准免责声明:函数已弃用。使用或。使用此查询

SELECT `status_id`, 
       max(`id`) as `max` 
FROM table 
GROUP BY `status_id`
这将为您提供所有成对的
status\u id
和相应的
max(id)
per
status\u id
。然后,您可以在常规(
while
)循环中获取结果


标准免责声明:函数已弃用。使用或代替。

使用
分组依据

select status_id, max(id) as maxValue FROM `table` 
where status_id in (1,2,3,4) group by status_id

使用
分组依据

select status_id, max(id) as maxValue FROM `table` 
where status_id in (1,2,3,4) group by status_id

旁注:停止使用不推荐的
mysql.*
函数。请改用MySQLi或PDO。侧注:停止使用已弃用的
mysql.*
函数。改用MySQLi或PDO。这应该是答案。我不知道对方的选票是从哪里来的。这应该是答案。我不知道对方的选票是从哪里来的。