相关子查询嵌套(第二顺序)无法访问主查询(MySQL)

时间:2015-06-17 22:23:20

标签: mysql

我正在尝试做这样的事情

编辑 - 整个查询。

SELECT *
FROM ride
WHERE
(
    SELECT COUNT(*) FROM (
        SELECT DISTINCT mobile FROM contacts WHERE `contacts`.`user_mobile` =  '".$user_mobile."'
        UNION ALL
        SELECT DISTINCT mobile FROM contacts WHERE `contacts`.`user_mobile` = `ride`.`user_mobile`
    ) AS c
) > 1
AND ( 
    distance_geo('".$start_lat."', '".$start_long."', starting_lat, starting_long) < '".$start_radius."'
    OR id IN (
        SELECT rideid FROM routept WHERE 
        distance_geo('".$start_lat."', '".$start_long."', starting_lat, starting_long) < '".$start_radius."'
    )
)
AND ( 
    distance_geo('".$end_lat."', '".$end_long."', end_lat, end_long) < '".$end_radius."'
    OR id IN (
        SELECT rideid FROM routept WHERE 
        distance_geo('".$start_lat."', '".$start_long."', starting_lat, starting_long) < '".$end_radius."'
    )
)
AND availablity >= 1
AND start_time BETWEEN '".$start_after."' AND '".$start_before."'
AND start_time > NOW()
ORDER BY start_time ASC"

但是,我无法做到。我正在

Unknown column 'ride.user_mobile' in 'where clause'

我认为无法在嵌套子查询中访问它。我需要一个解决方法。我在这个where子句中有更多条件,但我只发布了令人不安的部分!

2 个答案:

答案 0 :(得分:1)

试试这个,让我知道

Select  contacts.user_mobile  // here you can add other columns
        , Count(*) as Total
From    ride 
  JOIN mobile On contacts.user_mobile = ride.user_mobile
Where contacts.user_mobile = ".$user_mobile."
Group By contacts.user_mobile
Having Total > 1

更新

上面的Querty是你的基础,现在只需在Where子句中添加其他条件

Select  contacts.user_mobile  // here you can add other columns
        , Count(*) as Total
From    ride 
  JOIN mobile On contacts.user_mobile = ride.user_mobile
Where contacts.user_mobile = ".$user_mobile." 
    AND  distance_geo('".$start_lat."', '".$start_long."', 
            starting_lat, starting_long) < '".$start_radius."'
    OR ride.id IN (
        SELECT  rideid 
        FROM    routept 
        WHERE   distance_geo('".$start_lat."', '".$start_long."', 
                starting_lat, starting_long) < '".$start_radius."')
    AND  distance_geo('".$end_lat."', '".$end_long."', 
            end_lat, end_long) < '".$end_radius."'
    OR ride.id IN (
        SELECT  rideid 
        FROM    routept 
        WHERE distance_geo('".$start_lat."', '".$start_long."', 
                starting_lat, starting_long) < '".$end_radius."')
    AND ride.availablity >= 1
    AND ride.start_time BETWEEN '".$start_after."' AND '".$start_before."'
    AND ride.start_time > NOW()
Group By contacts.user_mobile
Having Total > 1
Order BY ride.start_time ASC"

答案 1 :(得分:0)

@The给了我一个有效的解决方案!

工作解决方案:

SELECT  contacts.user_mobile , ride.id, ride.user_mobile, ride.vehicle_no, ride.availablity, ride.ride_type, ride.starting_lat,
                        ride.starting_long, ride.starting_addr, ride.start_city, ride.end_lat, ride.end_long, ride.end_addr, ride.end_city,
                        ride.vehicle_ac, ride.status, ride.rent, ride.start_time, ride.dept_time,
                        COUNT(*) as Total
                FROM ride 
                    JOIN contacts ON contacts.user_mobile = ride.user_mobile
                WHERE contacts.user_mobile = ".$user_mobile."
                AND ( 
                    distance_geo('".$start_lat."', '".$start_long."', ride.starting_lat, ride.starting_long) < '".$start_radius."'
                    OR ride.id IN (
                        SELECT rideid FROM routept WHERE 
                        distance_geo('".$start_lat."', '".$start_long."', ride.starting_lat, ride.starting_long) < '".$start_radius."'
                    )
                )
                AND ( 
                    distance_geo('".$end_lat."', '".$end_long."', ride.end_lat, ride.end_long) < '".$end_radius."'
                    OR ride.id IN (
                        SELECT rideid FROM routept WHERE 
                        distance_geo('".$start_lat."', '".$start_long."', ride.end_lat, ride.end_long) < '".$end_radius."'
                    )
                )
                AND ride.availablity >= 1
                AND ride.start_time BETWEEN '".$start_after."' AND '".$start_before."'
                AND ride.start_time > NOW()
                GROUP BY contacts.user_mobile
                HAVING Total > 1
                ORDER BY ride.start_time ASC"

谢谢@The。