简体   繁体   中英

SQL - using result from subquery in another 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            

My question is is it possible ( the above does not work as is ) to use the results from the first subquery to compare against in the second? (ie job_id = jobs.job_id )

Regards

EDIT

(commenting here as there's more room)

    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   

My initial question was slightly incorrect. I actually want to get the amount of quotes foreach job, not the quote comments.

The above that was suggested below seemed to work at first glance but it returns the jobs that have more than one quote submited x amount of times with the quote count being 1 for each. Any ideas?

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

This will eliminate the subquery parsing and make your overall query run faster. I removed the quote job ID because we're joining on it and therefore only need to pull it once. I notice we're splicing directly from jobs to quote comments. You may want to link through quotes so that you see how many comments per quote there are instead of how many quote comments there are for the job overall

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

This will pull the Jobs that are authored by ID and then do a count on the quotes submitted for the job. Unfortunateley to get the count of quotes for the job, you have to leave off the engineer id and quote id from the quote table

Editing your first query:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM