我尝试使用php制作销售报告文档并从mysql数据库连接它。 如何在PHP get方法中使用where + group by + order(mysql query)? 我可以使用SELECT和WHERE,但是如何添加GROUP BY和ORDER BY查询?
$date=$_GET['date'];
$query=mysql_query("select * from product_sold where date=" . $date);
while($lihat=mysql_fetch_array($query)){
$pdf->Cell(1, 0.8, $no , 1, 0, 'C');
$pdf->Cell(3, 0.8, $lihat['date'],1, 0, 'C');
$pdf->Cell(6, 0.8, $lihat['product'],1, 0, 'C');
$pdf->Cell(3, 0.8, $lihat['quantity'], 1, 0,'C');
$pdf->Cell(4, 0.8, "Rp. ".number_format($lihat['price'])." ,-", 1, 0,'C');
你在sql语句的末尾使用GROUP BY和ORDER BY,就像这样......
"SELECT * FROM table WHERE date = $date ORDER BY desiredCategory "
此外,您真的需要使用预准备语句。您正在以这种方式开放sql注入攻击。
More about GROUP BY More about ORDER BY More about prepared statements
您可以在WHERE过滤后添加GROUP BY,然后可以添加ORDER BY。
SELECT *
FROM product_sold
WHERE date = '$date'
GROUP BY id
ORDER BY product ASC
并注意像@jtylerm这样的sql注入说。