简体   繁体   English

PHP MYSQL从多个列中检索Min值

[英]PHP MYSQL Retrieve Min value from several columns

Very new to PHP so pls bear with me. 非常新的PHP所以请耐心等待。

I have a table with several columns as such id | 我有一个表有几列的表,如id | onezw | onezw | twozw | twozw | threezw | threezw | fourzw | fourzw | fivezw | fivezw | sixzw | sixzw | minzw minzw

each column has a number that get entered, I then need the 'minzw' column to show the minimum value of the 6 columns. 每列都有一个输入的数字,然后我需要'minzw'列来显示6列的最小值。 I have the following code but just cant seem to get the problem. 我有以下代码,但似乎无法解决问题。 pls can someone nudge me in the right direction. 有人可能会朝着正确的方向推动我。

<?php
$host="localhost"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxxx"; // Database name
$tbl_name="ZW"; // Table name


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];

// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$minzwarray = array("onezw" , "twozw" , "threezw" , "fourzw" , "fivezw" , "sixzw");
$minzw = min($minzwarray);

$rows=mysql_fetch_array($result);
echo $minzw['minzw'];


mysql_close();
?>

You can use mysql to return an extra column with the min value using LEAST() : 您可以使用mysql使用LEAST()返回带有最小值的额外列:

SELECT *,LEAST(onezw , twozw , threezw , fourzw , fivezw , sixzw) AS MIN_VALUE
FROM tab1

sqlfiddle demo sqlfiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM