我的用例就是这样。
我希望将餐厅以及该餐厅中评分最高的商品带到一起。
我的伪查询将是这样的。
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body>
<div ng-controller="createCtrl as main">
<h1>create Ctrl</h1>
<input ng-model="title" placeholder="title" /><br/>
<input ng-model="name" placeholder="name" /><br/>
<input ng-model="post" placeholder="post" /><br/>
<input ng-model="tag" placeholder="tag" /><br/>
<button ng-click="display()"> SAVE DATA </button>
</div>
<br/> <hr/>
<div ng-controller="newsCtrl">
<h2>news Ctrl </h2>
<button ng-click="getData()"> GET DATA </button>
<p> title : {{array.title}} </p>
<p> name : {{array.name}} </p>
<p> post : {{array.post}} </p>
<p> tag : {{array.tag}} </p>
</div>
</body>
</html>
在RESTAURANTS表中。
SELECT * FROM RESTAURANTS
WHERE RESTAURANT_IDS = 1,2,3,4,5
LEFT JOIN RESTAURANT_ITEMS
WHERE ratings = MAX(ratings)
在RESTAURANT_ITEMS表中
id|name
-------------
1 |McDonalds
2 |Pizzahut
3 |Buger king
4 |KFC
所需的输出为
id|name |rating| FK_RES_ID
----------------------------
1 |Pizza |5 |2
2 |Lava cake |4 |2
3 |Veg burger|3 |4
4 |Chicken b |5.6 |4
5 |Ice cream |4.3 |1
6 |Burger |5 |1
答案 0 :(得分:0)
我想这就是您要寻找的。尝试并确认。 :)
SELECT
res.ResName
,item.Name as ResItem
,(Select max(SubItem.rating) from RESTAURANT_ITEMS SubItem where res.RestaurantID= SubItem.RestaurantID) as Rating
FROM
RESTAURANTS res
LEFT JOIN
RESTAURANT_ITEMS item on res.RestaurantID = item.RestaurantID
WHERE
res.Restaurant_ID in (1,2,3,4,5)
问题后的编辑部分也已更新
SELECT
res.Name as [Restaurant Name]
,item.Name as [Top Item]
FROM
RESTAURANTS res
LEFT JOIN
RESTAURANT_ITEMS item on res.RestaurantID = item.RestaurantID
and rating = (select max(subItems.rating) from RESTAURANT_ITEMS subItems where subItems.RestaurantID = res.RestaurantID)
WHERE
res.Restaurant_ID in (1,2,3,4,5)
希望这对您有所帮助:)
答案 1 :(得分:0)
尝试以下操作:
SELECT r.id,
r.name,
ri.name AS top_item
FROM RESTAURANTS AS r
LEFT JOIN (SELECT FK_RES_ID, MAX(rating) AS max_rating
FROM RESTAURANT_ITEMS
GROUP BY FK_RES_ID) AS dt
ON dt.FK_RES_ID = r.id
LEFT JOIN RESTAURANT_ITEMS AS ri ON ri.FK_RES_ID = dt.FK_RES_ID AND
ri.rating = dt.max_rating
WHERE r.id IN (1,2,3,4)