Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法在mysql select查询中获取两个字段_Php_Mysql - Fatal编程技术网

Php 无法在mysql select查询中获取两个字段

Php 无法在mysql select查询中获取两个字段,php,mysql,Php,Mysql,我的路由表中有四个字段 busid、routid、位置和距离 我想显示一个select查询的busid和距离。我的选择查询如下: $endb = mysql_query("select case when a.position < b.position then a.busid when a.position > b.position then a.busid else null end as busid, a.distance as distance from (select bu

我的路由表中有四个字段

busid、routid、位置和距离

我想显示一个select查询的busid和距离。我的选择查询如下:

$endb = mysql_query("select case when a.position < b.position then a.busid when a.position > b.position then a.busid else null end as busid, a.distance as distance from (select busid,position from route where routid=$result2) a join (select busid,position from route where routid=$end) b on a.busid = b.busid") or die(mysql_error());   

但当我使用这个查询时,它给出了一个错误:字段列表中的未知字段距离。请帮助我在子查询a中缺少什么

缺少距离

select 
    case 
        when a.position < b.position then a.busid 
        when a.position > b.position then a.busid 
        else null 
    end as busid, 
    a.distance as distance 
from (
    select busid, position, distance
    from route 
    where routid=$result2
) as a join (
    select busid, position 
    from route 
    where routid=$end
) as b 
on a.busid = b.busid
甚至更好的版本:

SELECT if (a.position <> b.position, a.busid, null) busid, a.distance
FROM  route a, route b
WHERE a.busid = b.busid
AND   a.routid= $result2
AND   b.routid= $end

子查询a中缺少距离

select 
    case 
        when a.position < b.position then a.busid 
        when a.position > b.position then a.busid 
        else null 
    end as busid, 
    a.distance as distance 
from (
    select busid, position, distance
    from route 
    where routid=$result2
) as a join (
    select busid, position 
    from route 
    where routid=$end
) as b 
on a.busid = b.busid
甚至更好的版本:

SELECT if (a.position <> b.position, a.busid, null) busid, a.distance
FROM  route a, route b
WHERE a.busid = b.busid
AND   a.routid= $result2
AND   b.routid= $end

除非在MySQL中有必要,否则不应该在from子句中使用子查询。它们阻止优化器生成最佳查询计划

编写查询的更好方法:

select (case when a.position < b.position then a.busid
             when a.position > b.position then a.busid 
        end) as busid,
       a.distance
from route a join
     route b
     on a.busid = b.busid and
        a.routid = $result2 and b.routid = $end;

当然,您的具体问题是a.distance没有定义,因为它没有在子查询中定义。

除非在MySQL中有必要,否则不应该在from子句中使用子查询。它们阻止优化器生成最佳查询计划

编写查询的更好方法:

select (case when a.position < b.position then a.busid
             when a.position > b.position then a.busid 
        end) as busid,
       a.distance
from route a join
     route b
     on a.busid = b.busid and
        a.routid = $result2 and b.routid = $end;


当然,您的具体问题是a.distance没有定义,因为它没有在子查询中定义。

我在字段列表中有distance哦,好的。我没有对您的子查询没有字段进行否决,请将该字段添加到别名的子查询中,然后重试!我如何定义这个请帮助我你需要在子查询中选择距离。我在字段列表中有距离哦,好的。我没有对您的子查询没有字段进行否决,请将该字段添加到别名的子查询中,然后重试!我如何定义这个请帮助我你也需要在子查询中选择距离。关键字AS是可选的,不需要。可能是上面提到的打字错误。Yes关键字对于阅读本文的任何人来说都是可选的:虽然解决方案在技术上是正确的,但它不是编写查询的最佳方式。MySQL具体化子查询,这会产生开销并阻止使用索引进行进一步处理。关键字AS是可选的,不需要。可能是上面提到的打字错误。Yes关键字对于阅读本文的任何人来说都是可选的:虽然解决方案在技术上是正确的,但它不是编写查询的最佳方式。MySQL具体化子查询,这会产生开销并阻止使用索引进行进一步处理。当a.position b.position.Yes这个答案更好+1时,可以通过单个不等式比较进一步简化案例表达式。当a.position b.position时,可以通过单个不等式比较进一步简化CASE表达式。