SELECT A.* , B.* FROM
(SELECT ID,DATE FROM APPLE) A
INNER JOIN
(SELECT ID,MAX(DATE) AS MAXDATE FROM APPLE GROUP BY ID) A1
ON A.ID = A.ID AND A.DATE = A1.MAXDATE
WHERE A.DATE > CURRENT_DATE
LEFT OUTER JOIN (
SELECT ID,NAME FROM BANANA) B
ON A.ID = B.ID
WHERE B.NAME IN ('USA','GBR') LIMIT 10;
错误:编译语句时出错:FAILED:ParseException行 22:0在'CURRENT_DATE'附近的'LEFT'处缺少EOF (状态= 42000,代码= 40000)
答案 0 :(得分:0)
WHERE子句,即A.DATE > CURRENT_DATE
应位于第一个选择内。另请注意,您的条件为A.ID = A.ID
,而不是A.ID = A1.ID
SELECT
A.* , B.*
FROM
(SELECT ID,DATE FROM APPLE WHERE DATE > CURRENTDATE) A
INNER JOIN
(SELECT ID,MAX(DATE) AS MAXDATE FROM APPLE GROUP BY ID) A1
ON
A.ID = A1.ID AND A1.DATE = A1.MAXDATE
LEFT OUTER JOIN
(SELECT ID,NAME FROM BANANA) B
ON
A.ID = B.ID
WHERE B.NAME IN ('USA','GBR') LIMIT 10;
答案 1 :(得分:0)
您的问题是您在SQL语句中间有一个WHERE子句。您可以将其移动到A的嵌套查询中,也可以将其添加到最后的WHERE子句中。您可能还希望在嵌套查询中移动B表上的过滤,因为您实际上是通过将左连接放在语句末尾的WHERE子句中而将其设置为内连接。
或者
SELECT A.* , B.* FROM
(SELECT ID,DATE FROM APPLE WHERE DATE > CURRENT_DATE) A
INNER JOIN
(SELECT ID,MAX(DATE) AS MAXDATE FROM APPLE GROUP BY ID) A1
ON A.ID = A.ID AND A.DATE = A1.MAXDATE
LEFT OUTER JOIN (
SELECT ID,NAME FROM BANANA WHERE NAME IN ('USA','GBR') ) B
ON A.ID = B.ID
LIMIT 10;
或
SELECT A.* , B.* FROM
(SELECT ID,DATE FROM APPLE) A
INNER JOIN
(SELECT ID,MAX(DATE) AS MAXDATE FROM APPLE GROUP BY ID) A1
ON A.ID = A.ID AND A.DATE = A1.MAXDATE
LEFT OUTER JOIN (
SELECT ID,NAME FROM BANANA WHERE NAME IN ('USA','GBR') ) B
ON A.ID = B.ID
WHERE A.DATE > CURRENT_DATE
LIMIT 10;