选择最小值时遇到问题。我已经创建了内部联接与2表和SQL查询中的简单计算。现在我想从计算中选择最小值。在这里我的查询..
$sql = "SELECT jobmatl_mst.item as item2,jobmatl_mst.u_m, job_mst.item, jobmatl_mst.job, jobmatl_mst.item as item2, jobmatl_mst.description, jobmatl_mst.matl_qty_conv , itemwhse_mst.item as whseitem,itemwhse_mst.qty_on_hand, itemwhse_mst.qty_on_hand / jobmatl_mst.matl_qty_conv as total
FROM job_mst
INNER JOIN jobmatl_mst ON job_mst.job=jobmatl_mst.job
INNER JOIN itemwhse_mst ON jobmatl_mst.item=itemwhse_mst.item
WHERE job_mst.item ='601010944003' AND job_mst.type='S'
ORDER BY total";
我想找到“总数”的最小值..
答案 0 :(得分:0)
更新:以下解决方案特定于MySQL。
<小时/> 您可以获得的最接近您自己的代码的答案是在查询结束时使用$sql = "
SELECT
jobmatl_mst.item as item2,
jobmatl_mst.u_m,
job_mst.item,
jobmatl_mst.job,
jobmatl_mst.item as item2,
jobmatl_mst.description,
jobmatl_mst.matl_qty_conv ,
itemwhse_mst.item as whseitem,
itemwhse_mst.qty_on_hand,
itemwhse_mst.qty_on_hand / jobmatl_mst.matl_qty_conv as total
FROM job_mst
INNER JOIN jobmatl_mst
ON job_mst.job=jobmatl_mst.job
INNER JOIN itemwhse_mst
ON jobmatl_mst.item=itemwhse_mst.item
WHERE
job_mst.item ='601010944003' AND
job_mst.type='S'
ORDER BY total # Sort the result in a
# way that lowest is first and highest is last
LIMIT 1 # Then we need the first value
";
。
MIN()
也可以使用聚合函数group by
和$sql = "
SELECT
jobmatl_mst.item as item2,
jobmatl_mst.u_m,
job_mst.item,
jobmatl_mst.job,
jobmatl_mst.item as item2,
jobmatl_mst.description,
jobmatl_mst.matl_qty_conv ,
itemwhse_mst.item as whseitem,
itemwhse_mst.qty_on_hand,
MIN(itemwhse_mst.qty_on_hand / jobmatl_mst.matl_qty_conv) as min_total
FROM job_mst
INNER JOIN jobmatl_mst
ON job_mst.job=jobmatl_mst.job
INNER JOIN itemwhse_mst
ON jobmatl_mst.item=itemwhse_mst.item
WHERE
job_mst.item ='601010944003' AND
job_mst.type='S'
GROUP BY
jobmatl_mst.item as item2,
jobmatl_mst.u_m,
job_mst.item,
jobmatl_mst.job,
jobmatl_mst.item as item2,
jobmatl_mst.description,
jobmatl_mst.matl_qty_conv ,
itemwhse_mst.item as whseitem,
itemwhse_mst.qty_on_hand
";
:
GROUP BY
请注意,在这种情况下,除了具有聚合函数的列之外的所有列都在pageA[\adminlogin]
子句中。