简体   繁体   中英

Create Subquery Select for COUNT with JOIN inside the subquery

Have this COUNT subquery but I can't get the syntax to work .

    SELECT products.client_id,
       clients.name AS client_name,
       cars.vin,
       cars.make,  
    cars.model
   (SELECT COUNT(*) FROM manheim_auction_listings AS listings_sub
        JOIN products ON 
          manheim_auction_listings.product_id  = products.id 
        JOIN product_purchases ON
           products.current_product_purchase_id = product_purchases.id 
       WHERE listings_sub.car_id = manheim_auction_listings.car_id AND                      
          listings_sub.id <> manheim_auction_listings.id and         
          manheim_auction_listings.product_purchase_id = 
          product_purchases.id) as previous_auction_count 
      FROM manheim_auction_listings
      JOIN cars ON 
      cars.id = manheim_auction_listings.car_id .....

The (SELECT COUNT(*) will not pass syntax with the JOIN's I Need to get the right count.

You are missing GROUP BY in your inner query.

WHERE listings_sub.car_id = manheim_auction_listings.car_id AND                      
      listings_sub.id <> manheim_auction_listings.id and         
      manheim_auction_listings.product_purchase_id = 
      product_purchases.id
      GROUP BY <list_of_column(s)>

Also, there are couple more syntax error(s)

You are missing comma , just after cars.model column MUST be separated by , .

 cars.model
  (SELECT COUNT(*) FROM manheim_auction_listings AS listings_sub

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