从数据库行中选择最低价格并忽略 null 或 0
我有一个名为“价格”的数据库,其中有 12 列。
comp1、comp2、comp3、comp4、comp5、comp6 等。
然后我想运行一个查询来查找存储在与 id 匹配的行中的最低价格,但是我认为它将 null 视为最低值,因为我没有得到正确的答案。
无论如何?或者我做错了吗?
$query = "SELECT * FROM Prices WHERE id =$id" or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$comp1= $row['comp1'];
$comp2= $row['comp2'];
$comp3= $row['comp3'];
$comp4= $row['comp4'];
$comp5= $row['comp5'];
$comp6= $row['comp6'];
$comp7= $row['comp7'];
$comp8= $row['comp8'];
$comp9= $row['comp9'];
$comp10= $row['comp10'];
$comp11= $row['comp11'];
$comp12= $row['comp12'];
$min = array( $comp1, $comp2, $comp3, $comp4, $comp5, $comp6, $comp7, $comp8, $comp8, $comp10, $comp11, $comp12);
echo min($min);
}
I have a database called prices and there are 12 columns lets say.
comp1, comp2, comp3, comp4, comp5, comp6 and so on.
I then want to run a query to find the lowest price stored in that row that matches an id, however I think it is treating null as lowest value as I am not getting the correct answer.
Anyway around this? or am i doing this wrong?
$query = "SELECT * FROM Prices WHERE id =$id" or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$comp1= $row['comp1'];
$comp2= $row['comp2'];
$comp3= $row['comp3'];
$comp4= $row['comp4'];
$comp5= $row['comp5'];
$comp6= $row['comp6'];
$comp7= $row['comp7'];
$comp8= $row['comp8'];
$comp9= $row['comp9'];
$comp10= $row['comp10'];
$comp11= $row['comp11'];
$comp12= $row['comp12'];
$min = array( $comp1, $comp2, $comp3, $comp4, $comp5, $comp6, $comp7, $comp8, $comp8, $comp10, $comp11, $comp12);
echo min($min);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您在调用
min()
之前的数组,您将清除任何计算结果为 false 的内容,包括null
和0
。例如:$min = min(array_filter($min));
If you call
array_filter()
on the array before callingmin()
, you will purge anything that evaluates to false, includingnull
and0
.For example:
$min = min(array_filter($min));
尝试:
Try:
来自 http://php.net/manual/en/function.min.php 中的评论:
我已经修改了无错误最小版本以忽略 NULL 值(否则返回 0)。
From a comment in http://php.net/manual/en/function.min.php:
I've modified the bugfree min-version to ignore NULL values (else it returns 0).
试试这个:
Try this: