2017-08-23 5 views
0

テーブルと列は、私はブランド+在庫からバイクショップから利用できる最も総株式すなわち株式で自転車を選択したいこのselect文はどのようにすればよいですか?

Bikers(BikerName, Country, Age, Category, World_Cup_Wins, 

World_Championship_Wins, Brand, Bike) 

BikerMeetUp(BikerName, Date, Location) 

BikeShopInfo(BikeShopName, Telephone_Number, Address, Website) 

Brands(BrandName, Website, Model) 

Bikes(Model, Price, Weight, Colour, Rating, State) 

BikeTyre(Model, Tyre_Model, Size) 

BikeForks(Model, Fork_Model, Travel) 

BikeBrandSellStock(Model, Size, BrandStock) 

BikeShopSellBike(BikeShopName, Model, Size, BikeShopStock) 

下にありますか?これはどうすればいいですか?これは私が今までに持っていて実行していないものです。

Select model, price, rating, state, brands.brandname, brands.website, 
bikebrandsellstock.size, bikebrandsellstock.brandstock, bikeshopsellbike.bikeshopname, bikeshopsellbike.size, bikeshopsellbike.bikeshopstock 
from bikes 
INNER JOIN brands ON bikes.model = brands.model 
INNER JOIN bikebrandsellstock ON bikes.model = bikebrandsellstock.model 
INNER JOIN bikeshopsellbike ON bikes.model = bikeshopsellbike.model 
WHERE SUM(bikebrandsellstock.stock+bikeshopsellbike.stock) 
IN (SELECT MAX(SUM(bikebrandsellstock.stock+bikeshopsellbike.stock))) 
+1

オメア、あなたの編集は回帰であり、元に戻しました。あなたはcoldspeedが入れたクエリフォーマットを破った –

答えて

0

たぶん、あなたはそれをoverthinkingている:(SELECT MAX(SUM(bikebrandsellstock.stock+bikeshopsellbike.stock)))

SELECT * 

FROM 
bikebrandsellstock b 
INNER JOIN 
bikeshopsellbike s 
ON s.model = b.model and s.size = s.size 

ORDER BY b.brandstock + s.bikeshopstock DESC 
LIMIT 1 
0

あなたの構文が間違っている、あなたはFROMの文が欠落しています。

+0

JCは親クエリのテーブルエイリアスを使用しているので、サブクエリがうまくいくと思われます。あなたの答えは、なぜこれがこの文脈ではうまくいかないのかといういくつかの説明から利益を得るかもしれませんか? –

0
SELECT model 
    , SUM(stock) total 
    FROM 
    (SELECT Model, BrandStock stock FROM BikeBrandSellStock 
     UNION 
     SELECT Model, BikeShopStock FROM BikeShopSellBike 
    ) x 
GROUP 
    BY model 
ORDER 
    BY total DESC 
LIMIT 1; 

この解決策では、結びつきは考慮されません。

関連する問題