SQL内连接语法错误

时间:2015-11-10 06:31:46

标签: mysql sql

我有这个错误。我是SQL的新手,无法弄清楚我的语法有什么问题。我将INTERSECT语句更改为内连接,意识到SQL不接受这样的语法。但是我继续收到错误。

错误:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use n
ear 'a
INNER JOIN
(Select h.propertyId as id
From House h, has_weather hw, weather_ye' at line 8

该查询用于查找用户ID和用户的电子邮件,其属性的评级等于5且AvgTemp超过55.

    Select has_property.userId
    From has_property 
    Where has_property.propertyId IN
    (
        (Select hRR.propertyId as id
        From hasRatings_Rate hRR
        Where hRR.ratingId = 5
        ) a

        INNER JOIN

        (Select h.propertyId as id
        From House h, has_weather hw, weather_year wy
        Where hw.weatherId = wy.weatherId AND hw.homeAddId = h.homeAddId AND wy.AvgTemp > 55
        )b
        ON (a.id = b.id)

);

5 个答案:

答案 0 :(得分:3)

试试这个:

Select has_property.userId
From has_property hp
JOIN hasRatings_Rate hRR ON hp.propertyId = hRR.propertyId
JOIN House h ON h.id = hRR.id
JOIN has_weather hw ON hw.homeAddId = h.homeAddId
JOIN weather_year wy ON hw.weatherId = wy.weatherId
WHERE hRR.ratingId = 5
AND  wy.avgTemp > 55

不需要任何子查询,只需要一连串的连接。

答案 1 :(得分:1)

试试这个:

Select has_property.userId
From has_property 
INNER JOIN
(
    SELECT id 
    FROM
    (
    Select hRR.propertyId as id
    From hasRatings_Rate hRR
    Where hRR.ratingId = 5
    ) a
    INNER JOIN
    (
    Select h.propertyId as id
    From House h, has_weather hw, weather_year wy
    Where hw.weatherId = wy.weatherId AND hw.homeAddId = h.homeAddId AND wy.AvgTemp > 55
    ) b ON (a.id = b.id)
) c ON(has_property.propertyId = c.id)

答案 2 :(得分:1)

应该是

 Select has_property.userId
    From has_property 
    Where has_property.propertyId IN
    (
        SELECT a.id FROM //This line need to add
        (Select hRR.propertyId as id
        From hasRatings_Rate hRR
        Where hRR.ratingId = 5
        ) a

        INNER JOIN

        (Select h.propertyId as id
        From House h, has_weather hw, weather_year wy
        Where hw.weatherId = wy.weatherId AND hw.homeAddId = h.homeAddId AND wy.AvgTemp > 55
        )b
        ON (a.id = b.id)

);

答案 3 :(得分:1)

您可以尝试这样:

Select has_property.userId
From has_property 
Where has_property.propertyId IN
(
   Select hRR.propertyId as id
   From hasRatings_Rate hRR INNER JOIN House h on hRR.propertyId = h.propertyId
      INNER JOIN has_weather hw on hw.homeAddId = h.homeAddId
      INNER JOIN weather_year wy on hw.weatherId = wy.weatherId
   Where hRR.ratingId = 5 and wy.AvgTemp > 55
) 

此外,请尽量避免使用逗号分隔的JOIN。

答案 4 :(得分:1)

你可以通过组合连接和比较来简单地获取内部查询的结果 外部查询

Select has_property.userId
From has_property 
Where has_property.propertyId IN
(
    select hRR.propertyId as id 
    From hasRatings_Rate hRR join house h on h.id = hrr.uid and hrr.ratingid = 5
    join has_weather hw on hw.homeaddid = h.homeaddid
    join weather_year wy on hw.weatherid = wy.weatherid AND wy.AvgTemp > 55
) 

了解更多信息types of sql server joins with syntax and example