MAX和COUNT(*)查询SQL

时间:2018-07-01 19:23:01

标签: php mysql

我想根据“ codp”进行报告。 我想显示仅包含该“ codp”(这是最常见的)的“ fiser”和“ comenzi”表条目。您能帮我这个疑问吗?查询必须在选定的时间段后计算出“ fiser”表中大部分时间出现了什么“ codp”。

谢谢。

<?php 

	if(isset($_POST['add'])){   

	$codr = mysqli_real_escape_string($conn, $_POST['codr']); 
$codp = mysqli_real_escape_string($conn, $_POST['codp']); 
$codc = mysqli_real_escape_string($conn, $_POST['codc']); 

		$sql = "SELECT a.codr, a.datar, b.codc , c.denp, MAX(c.codp)
		FROM (SELECT COUNT(*) AS c.codp FROM fiser a) 
		JOIN comenzi b ON a.codc=b.codc
		 JOIN produse c ON b.codp=c.codp
		WHERE  a.datar>=STR_TO_DATE('".$_POST['d1']."','%Y-%m-%d') AND a.datar<=STR_TO_DATE('".$_POST['d2']."','%Y-%m-%d') 
		AND a.codr='$codr' AND b.codc='$codc' AND c.codp='$codp' ";
		
		$result = mysqli_query($conn, $sql);
		$queryResult = mysqli_num_rows($result);

		if($queryResult > 0 ){
while ($row = mysqli_fetch_assoc($result)) { 
echo "<table border=1 >
<tr>
    <td><p> ".$row['codp']." </p></td>
    <td><p> ".$row['denp']." </p></td> 
    <td><p> " .$row['codr']." </p></td>
 <td><p> " .$row['datar']." </p></td>
  <td><p> " .$row['codc']." </p></td>
  </tr> </table> ";
			} 	
			
		} else { echo " No results!" ;
		
		}
 }

	?>

0 个答案:

没有答案