内部联接的MySQL查询语法错误

时间:2015-11-19 09:21:00

标签: mariadb

我有以下查询:

(
 SELECT * FROM salon_promo tt 
   INNER JOIN 
    (SELECT salon_id, MAX(promo_id) AS MaxDateTime 
       FROM salon_promo 
       GROUP BY salon_id) 
     groupedtt ON tt.salon_id = groupedtt.salon_id 
       AND tt.promo_id = groupedtt.MaxDateTime) 
   INNER JOIN salons USING (salon_id)

并回顾以下错误消息:

  

错误:您的SQL语法有错误;检查与您的MariaDB服务器版本对应的手册,以便在#ING; INNER JOIN沙龙USING(salon_id)'附近使用正确的语法。在第1行

当我使用以下查询时:

SELECT * from salon_promo INNER JOIN salons USING (salon_id)

一切正常。并从():

查询
(
 SELECT * FROM salon_promo tt 
   INNER JOIN 
    (SELECT salon_id, MAX(promo_id) AS MaxDateTime 
       FROM salon_promo 
       GROUP BY salon_id) 
     groupedtt ON tt.salon_id = groupedtt.salon_id 
       AND tt.promo_id = groupedtt.MaxDateTime
)

工作正常。什么可能导致这个问题?

1 个答案:

答案 0 :(得分:2)

您的查询错误

groupedtt.MaxDateTime) 
                     ^^ )should remove

应该是

groupedtt.MaxDateTime

所以最后的查询是

SELECT * 
FROM salon_promo tt 
INNER JOIN (
    SELECT salon_id, MAX(promo_id) AS MaxDateTime 
    FROM salon_promo 
    GROUP BY salon_id) 
groupedtt ON tt.salon_id = groupedtt.salon_id 
AND tt.promo_id = groupedtt.MaxDateTime 
INNER JOIN salons USING (salon_id)