简体   繁体   中英

MySQL/SQL - When are the results of a sub-query avaliable?

Suppose I have this query

SELECT * FROM (
 SELECT * FROM table_a   
   WHERE id > 10 )
  AS a_results LEFT JOIN 
   (SELECT * from table_b
      WHERE id IN 
       (SElECT id FROM a_results)

  ON (a_results.id = b_results.id)

I would get the error "a_results is not a table". Anywhere I could use the re-use the results of the subquery?

Edit: It has been noted that this query doesn't make sense...it doesn't, yes. This is just to illustrate the question which I am asking; the 'real' query actually looks something like this:

      SELECT SQL_CALC_FOUND_ROWS * FROM
            (  SELECT wp_pod_tbl_hotel . *
            FROM wp_pod_tbl_hotel, wp_pod_rel, wp_pod
            WHERE wp_pod_rel.field_id =12
            AND wp_pod_rel.tbl_row_id =1
            AND wp_pod.tbl_row_id = wp_pod_tbl_hotel.id
            AND wp_pod_rel.pod_id = wp_pod.id
        )  as
              found_hotel LEFT JOIN (

    SELECT COUNT(*) as review_count, avg( (
                    location_rating + staff_performance_rating + condition_rating + room_comfort_rating + food_rating + value_rating
                                        ) /6 ) AS average_score, hotelid
                                        FROM (

                                            SELECT r. * , wp_pod_rel.tbl_row_id AS hotelid
                                                FROM wp_pod_tbl_review r, wp_pod_rel, wp_pod
                                                WHERE wp_pod_rel.field_id =11
                                                AND wp_pod_rel.pod_id = wp_pod.id
                                                AND r.id = wp_pod.tbl_row_id
                                                AND wp_pod_rel.tbl_row_id
                                                IN (                                                

SELECT wp_pod_tbl_hotel .id
            FROM wp_pod_tbl_hotel, wp_pod_rel, wp_pod
            WHERE wp_pod_rel.field_id =12
            AND wp_pod_rel.tbl_row_id =1
            AND wp_pod.tbl_row_id = wp_pod_tbl_hotel.id
            AND wp_pod_rel.pod_id = wp_pod.id

                                                    )
      ) AS hotel_reviews
           GROUP BY hotel_reviews.hotelid
          ORDER BY average_score DESC
                 AS sorted_hotel ON (id = sorted_hotel.hotelid)

As you can see, the sub-query which makes up the found_query table is repeated elsewhere downward as another sub-query, so I was hoping to re-use the results

You can not use a sub-query like this.

I'm not sure I understand your query, but wouldn't that be sufficient?

SELECT * FROM table_a a
LEFT JOIN table_b b ON ( b.id = a.id )
WHERE a.id > 10

It would return all rows from table_a where id > 10 and LEFT JOIN rows from table_b where id matches.

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