我有几张桌子: 第一个:所有者
<div class="container">
<div class="inner" style="background-color:red;"></div>
<div class="inner" style="background-color:yellow;"></div>
<div class="inner" style="background-color:black;"></div>
</div>
第二个: Service_Done
div
第三个:
Owner_ID Owner_Name
1 Sam
2 Andrea
3 Gabriela
我有这段代码:
Puppy_ID servided_date
1 01/25/2012
2 02/18/2012
3 05/14/2012
从这里我被困住了。我在想桌子之间的连接,但我不确定
答案 0 :(得分:0)
以下将返回宠物服务最多的前5名业主。
SELECT
a.`Owner_ID`,
a.`Owner_Name`,
COUNT(*) as `Times_Serviced`
FROM `Owner` a
JOIN `Puppys` b
ON a.`Owner_ID` = b.`Owner_ID`
JOIN `Service_Done` c
ON c.`Puppy_ID` = b.`Puppy_ID`
GROUP BY a.`Owner_ID`
ORDER BY count(*) DESC
LIMIT 5;
通过更改组,您还可以返回最多服务的宠物。您还可以按年份(或其他时间段)对结果进行分组。
答案 1 :(得分:0)
上述查询将通过根据所有者对其进行分组并根据服务的发生次数(降序)对其进行分组,为您提供服务最多的幼犬。
SELECT Top 1 o.Owner_Name --Owner with most servicing
FROM Owner o
JOIN Puppys p ON o.Owner_ID = p.Owner_ID
JOIN Service_Done c ON c.Puppy_ID = p.Puppy_ID
GROUP BY o.Owner_ID --grouping based on owner
ORDER BY count(*) DESC --order in descending order on the count of occurences