[英]select max value from (select query)
I've got a SQL statement which already works, but I want to add another subselect query which returns (MAX value of results) for the following subselect query, 我有一个已经可以使用的SQL语句,但是我想添加另一个子选择查询,该查询为以下子选择查询返回(结果的MAX值),
(select count(*) from users where `score` =8 and `uni` = t.uni)*8 as 'rscore',
here is my code 这是我的代码
<?php
$connect = mysqli_connect('localhost', 'root', '', 'test')or die ( mysqli_error($connect) );
$output = '';
$search = mysqli_real_escape_string($connect, $_POST["query"]);
$query = "select t.uni,
(select count(*) from users where `score` =8 and `uni` = t.uni)*8 as 'rscore',
(select count(*) from users where `uni` = t.uni) as 'total'
from users t group by t.uni
";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
';
$i=1;
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td align="center">' . $i . '</td>
<td width="10%">'.$row["uni"].'</td>
<td align="center">'.$row["rscore"].'</td>
<td align="center">'.$row["total"].'</td>
</tr>
';
$i++;
}
}
?>
You could use an conditional aggregation
, count
and max
您可以使用
conditional aggregation
, count
和max
select uni, sum( case when score =8 then 1 else 0 end )*8 as rscore,
count(*) total,
max(my_col) my_max
from users
group by uni
Ref: 参考:
http://www.mysqltutorial.org/mysql-max-function/ http://www.mysqltutorial.org/mysql-max-function/
http://www.mysqltutorial.org/mysql-count/ http://www.mysqltutorial.org/mysql-count/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.