包含另一个子查询的内部联接中的子查询

时间:2011-06-28 10:59:09

标签: mysql

我编写了一个连接3个表的查询,我需要内连接2个表,然后将整个结果集内连接到另一个连接。

查询如下:

SELECT  R.ID,
        R.Title,
        R.Requirement_Text,
        R.Req_Owner,
        D.desname,
        D.team,
        D.stage,
        D.comm 
FROM Req R 
        LEFT JOIN 
        (
            SELECT  d1.ID AS 'id',
                    d1.designername AS 'desname',
                    d1.teamname AS 'team',
                    s.stage AS 'stage',
                    s.comments As 'comm' 
            FROM descomments d1 
                    LEFT JOIN stagecomments s ON d1.ID = s.ID 
                                                 AND d1.designername = s.designername
        ) D ON R.ID = D.id
WHERE R.ProjectID = 'STE 11.2' 
ORDER BY R.Priority

但是我收到了以下错误:

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT d1.ID AS 'id',d1.designername AS 'desname',d1.teamname A

4 个答案:

答案 0 :(得分:1)

第一次左连接后你不需要'descomments'..你实际上是加入D,这是你的子连接的结果..

SELECT 
  R.ID,
  R.Title,
  R.Requirement_Text,
  R.Req_Owner,
  D.designername,
  D.teamname,
  s.stage,
  s.comments 
FROM Req R LEFT JOIN descomments D ON R.ID=D.id
LEFT JOIN stagecomments s ON D.ID=s.ID AND D.designername=s.designername
WHERE 
  R.ProjectID='STE 11.2' 
ORDER BY R.Priority

答案 1 :(得分:0)

我不确定这是解决方案,但MySql不使用反引号`而不是'?

像这样:

SELECT  R.ID,
        R.Title,
        R.Requirement_Text,
        R.Req_Owner,
        D.desname,
        D.team,
        D.stage,
        D.comm 
FROM Req R 
        LEFT JOIN
        (
            SELECT  d1.ID AS `id`,
                    d1.designername AS `desname`,
                    d1.teamname AS `team`,
                    s.stage AS `stage`,
                    s.comments As `comm` 
            FROM descomments d1 
                    LEFT JOIN stagecomments s ON d1.ID = s.ID 
                                                 AND d1.designername = s.designername
        ) D ON R.ID = D.id
WHERE R.ProjectID = 'STE 11.2' 
ORDER BY R.Priority

答案 2 :(得分:0)

如果我正确地删除了其中出现的Java字符串分隔符和连接字符,则查询内容如下:

SELECT
    R.ID,
    R.Title,
    R.Requirement_Text,
    R.Req_Owner,
    D.desname,
    D.team,
    D.stage,
    D.comm
FROM
    Req R
    LEFT JOIN descomments ON
        (
            SELECT
                d1.ID AS 'id',
                d1.designername AS 'desname',
                d1.teamname AS 'team',
                s.stage AS 'stage',
                s.comments As 'comm'
            FROM
               descomments d1
               LEFT JOIN stagecomments s ON
                   d1.ID = s.ID AND
                   d1.designername = s.designername
        ) D ON R.ID = D.id
WHERE
    R.ProjectID = 'STE 11.2'
ORDER BY
    R.Priority

descomments ON之后的LEFT JOIN位错误。请尝试以下方法:

SELECT
    R.ID,
    R.Title,
    R.Requirement_Text,
    R.Req_Owner,
    D.desname,
    D.team,
    D.stage,
    D.comm
FROM
    Req AS R
    LEFT JOIN (
        SELECT
            d1.ID AS 'id',
            d1.designername AS 'desname',
            d1.teamname AS 'team',
            s.stage AS 'stage',
            s.comments AS 'comm'
        FROM
           descomments AS d1
           LEFT JOIN stagecomments AS s ON
               d1.ID = s.ID AND
               d1.designername = s.designername
    ) AS D ON R.ID = D.id
WHERE
    R.ProjectID = 'STE 11.2'
ORDER BY
    R.Priority

答案 3 :(得分:0)

从StevieG的重写查询开始,stageComments中的值从不在查询中使用 - 因此它似乎是多余的 - 但会显着减慢查询速度;以下内容应该更快地给出期望的结果:

SELECT 
  R.ID,
  R.Title,
  R.Requirement_Text,
  R.Req_Owner,
  D.desname,
  D.team,
  D.stage,
  D.comm 
FROM Req R LEFT JOIN descomments D ON R.ID=D.id
WHERE 
  R.ProjectID='STE 11.2' 
ORDER BY R.Priority;