我尝试从db获取数据。我可以得到最大值,但我无法列出x和y值。
代码如下。谢谢你的帮助
<?php
$sql = "SELECT * FROM veri UNION ALL SELECT MAX(x) as 'maxx', MIN(x) as 'minx', MAX(y) as 'maxy', MIN(y) as 'miny' FROM veri ";
$result = mysql_query($sql);
$result_array = array();
while($row = mysql_fetch_array($result)){
$result_array[] = $row;
echo $row['x'];
echo $row['y'];
echo $row['miny'];
echo "<br>";
echo $row['maxy'];
echo "<br>";
echo $row['minx'];
echo "<br>";
echo $row['maxx'];
echo "<br>";
}
?>
答案 0 :(得分:1)
我不确定我是否理解您对查询的意图,但如果veri有两列,则您的两个选择不兼容,因为您的第一个查询有两列而第二个查询有四列。查询与另一个查询i.f.f联合兼容。它们具有相同的列数,相应的列具有相同的类型。此外,查询的结果是一个表,因此您不能在不同行的列上使用不同的名称(x vs maxx)。
您可以为最小值和最大值使用四个子选项:
SELECT x, y, (SELECT MAX(x) FROM veri) as maxx
, (SELECT MIN(x) FROM veri) as minx
, (SELECT MAX(y) FROM veri) as maxy
, (SELECT MIN(y) FROM veri) as miny
FROM veri
或者您需要一个分类器来确定行代表的内容:
SELECT 'row' as origin, x, y FROM veri
UNION ALL
SELECT 'max' as origin, max(x) as x, max(y) as y FROM veri
UNION ALL
SELECT 'min' as origin, min(x) as x, min(y) as y FROM veri
但是,在这种情况下,最好将它分成两个查询:
q1: SELECT x, y FROM veri
q2: SELECT MAX(x) as maxx, MIN(x) as minx, MAX(y) as maxy, MIN(y) as miny FROM veri
并单独处理它们。
答案 1 :(得分:1)
好的,我把查询分成两个,就像你说的那样。它成功了。谢谢
$sql1= "SELECT x,y FROM veri ";
$result1=mysql_query($sql1) or die(mysql_error());
$result_array = array();
while($row1 = mysql_fetch_array($result1)){
$result_array[] = $row1;
echo $row1['x'];
echo $row1['y'];
}
$sql2= "SELECT MAX(x) as 'maxx', MIN(x) as 'minx', MAX(y) as 'maxy', MIN(y) as 'miny' FROM veri ";
$result2=mysql_query($sql2) or die(mysql_error());
$result_array = array();
while($row2 = mysql_fetch_array($result2)){
echo $row2['miny'];
echo "<br>";
echo $row2['maxy'];
echo "<br>";
echo $row2['minx'];
echo "<br>";
echo $row2['maxx'];
echo "<br>";
}