SQL - 在另一个子查询中使用子查询的结果

时间:2012-01-27 20:04:11

标签: sql subquery

SELECT 
    *
FROM 
    (
        SELECT 
            qu.job_id AS `qu_job_id` , qu.engineer_id, qu.id AS `quote_id` , jb.author, jb.job_id, jb.job_title, SUBSTRING( jb.job_description, 1, 200 ) AS `short_description` , jb.image_ref, jb.timestamp
        FROM 
            jobs AS `jb`
        LEFT JOIN 
            quotes AS `qu` 
        ON 
        qu.job_id = jb.job_id
        WHERE 
        jb.author = " . ID . "
        GROUP BY 
        jb.job_id
        ORDER BY 
        jb.timestamp DESC
    ) AS `jobs`
    LEFT JOIN 
    (
        SELECT 
        COUNT( id ) AS `total_replies` , job_id
        FROM 
        quote_comments 
        WHERE
        job_id = jobs.job_id
    ) AS `replies` 
    ON 
        replies.job_id = jobs.job_id            

我的问题是,使用第一个子查询的结果与第二个子查询进行比较是否可能(以上不能正常工作)? (即job_id = jobs.job_id

此致

修改

(此处评论有更多空间)

    SELECT 
    qu.engineer_id, 
    qu.id AS `quote_id`,
    jb.author,
    jb.job_id,
    jb.job_title,
    SUBSTRING( jb.job_description, 1, 200 ) AS `short_description` ,
    jb.image_ref,
    jb.timestamp,
    count(qu.id) AS comment_count
FROM 
    " . JOBS . " AS `jb`
LEFT JOIN 
    " . QUOTES . " AS `qu` 
ON 
    qu.job_id = jb.job_id 
WHERE 
    jb.author = " . ID . "
GROUP BY 
    jb.job_id,
    qu.engineer_id,
    qu.id,
    jb.author,
    jb.job_title,
    SUBSTRING( jb.job_description, 1, 200 ),
    jb.image_ref,
    jb.timestamp
ORDER BY 
    jb.timestamp DESC   

我的初步问题略有不正确。我实际上想要得到工作的报价数量,而不是报价评论。

以下建议的上述内容似乎乍一看似乎有效,但它会返回多个引用提交的作业x次,引号计数为1。有什么想法吗?

3 个答案:

答案 0 :(得分:0)

SELECT 
  qu.engineer_id, 
  qu.id AS `quote_id`,
  jb.author,
  jb.job_id,
  jb.job_title,
  SUBSTRING( jb.job_description, 1, 200 ) AS `short_description` ,
  jb.image_ref,
  jb.timestamp,
  count(qc.id) AS comment_count
FROM 
  jobs AS `jb`
LEFT JOIN quotes AS `qu` ON qu.job_id = jb.job_id
LEFT JOIN quote_comments AS `qc` ON qu.job_id = qc.job_id
WHERE 
  jb.author = " . ID . "
GROUP BY 
  jb.job_id,
  qu.engineer_id,
  qu.id,
  jb.author,
  jb.job_title,
  SUBSTRING( jb.job_description, 1, 200 )
  jb.image_ref,
  jb.timestamp
ORDER BY 
  jb.timestamp DESC

这将消除子查询解析并使整个查询运行得更快。我删除了引用作业ID,因为我们正在加入它,因此只需要拉一次。我注意到我们直接从工作中拼接引用评论。您可能希望通过引号链接,以便查看每个引用的注释数量,而不是整个作业的引用注释数量

答案 1 :(得分:0)

SELECT 
  jb.author,
  jb.job_id,
  jb.job_title,
  SUBSTRING( jb.job_description, 1, 200 ) AS `short_description` ,
  jb.image_ref,
  jb.timestamp,
  count(qu.id) AS quote_count
FROM 
  jobs AS `jb`
LEFT JOIN quotes AS `qu` ON qu.job_id = jb.job_id
WHERE 
  jb.author = " . ID . "
GROUP BY 
  jb.job_id,
  jb.author,
  jb.job_title,
  SUBSTRING( jb.job_description, 1, 200 )
  jb.image_ref,
  jb.timestamp
ORDER BY 
  jb.timestamp DESC

这将拉出由ID创作的作业,然后计算为作业提交的报价。不幸的是要获得工作报价的数量,你必须从报价表中取消工程师ID和报价ID

答案 2 :(得分:0)

编辑您的第一个查询:

SELECT 
    *
FROM 
    (   SELECT 
            qu.job_id AS `qu_job_id` , qu.engineer_id, qu.id AS `quote_id`,
            jb.author, jb.job_id, jb.job_title, 
            SUBSTRING( jb.job_description, 1, 200 ) AS `short_description`,
            jb.image_ref, jb.timestamp
        FROM 
            jobs AS `jb`
        LEFT JOIN 
            quotes AS `qu` 
        ON 
          qu.job_id = jb.job_id
        WHERE 
          jb.author = " . ID . "
        GROUP BY 
          jb.job_id
                               --- ORDER BY            --- not needed
                               --- jb.timestamp DESC   --- here
    ) AS `jobs`
    LEFT JOIN 
    (
        SELECT 
          COUNT( * ) AS `total_replies` , job_id
        FROM 
          quote_comments 
                                --- WHERE
                                --- job_id = jobs.job_id
        GROUP BY              --- replacing the WHERE 
          job_id              --- with a GROUP BY
    ) AS `replies` 
    ON 
        replies.job_id = jobs.job_id  
ORDER BY                        --- order clause 
  timestamp DESC                --- moved here