我有一个查询:
orp CategoryId Category City
999 1 bouquets city-a
999 2 arrengmnt city-b
999 4 chocolate city-c
我想在每一行中添加具有以下类别的商店数量:
orp CategoryId Category City NoShops
999 1 bouquets city-a 10
999 2 arrengmnt city-b 14
999 4 chocolate city-c 3
我需要获得CategoryId(@CategoryId),所以我在考虑这样的事情(显然不起作用):
SELECT Categories.OrderInPage as orp, CategoriesInLanguages.CategoryName, @CategoryId = Categories.Id
,(
SELECT COUNT(Shops.Id) as NoShops
FROM Cities INNER JOIN
CitiesInLanguages ON Cities.Id = CitiesInLanguages.CityId INNER JOIN
ShopsInCities ON Cities.Id = ShopsInCities.CityId INNER JOIN
CategoriesInLanguages INNER JOIN
Categories ON CategoriesInLanguages.CategoryId = Categories.Id INNER JOIN
ProductstInCategories ON Categories.Id = ProductstInCategories.CategoryId
WHERE (Products.IsEnable = 1) AND ( Categories.Id = @CategoryId)) AS Z
)
FROM Cities INNER JOIN
CitiesInLanguages ON Cities.Id = CitiesInLanguages.CityId INNER JOIN
ShopsInCities ON Cities.Id = ShopsInCities.CityId INNER JOIN
CategoriesInLanguages INNER JOIN
Categories ON CategoriesInLanguages.CategoryId = Categories.Id INNER JOIN
ProductstInCategories ON Categories.Id = ProductstInCategories.CategoryId
WHERE (Products.IsEnable = 1)
答案 0 :(得分:1)
您无法混合这两项操作。如果您尝试它,错误消息是不言自明的。
为变量赋值的SELECT语句不能是 结合数据检索操作。
答案 1 :(得分:0)
在MySQL中,您可以执行以下操作:
SELECT col1, col2, col3 FROM table1 WHERE col3 = param
,
但不能同时混合两个操作。我认为此规则适用于Sql Server。但是,现在很清楚你想要做什么。你能更具体一点吗?
答案 2 :(得分:0)
你只能拥有
Select col1, col2, col3 from table1
或
Select @param1 = col1, @param2 = col2, @param3 = col3 from table1 where id=value
而不是两者。注意我添加了WHERE子句,您的查询应返回1行以使用@param
如果您使用了第二个选项,则可以执行以下操作
Select @param1 as col1, @param2 as col2, @param3 as col3
返回结果