---
title: "两个表的查询 count "
author: "Lives"
date: "2011-09-15T10:02:57Z"
tags: []
categories: ["数据库"]
slug: "count"
draft: false
lastmod: "2011-09-15T10:02:57Z"
id: 1172237
url: "https://lives.iteye.com/blog/1172237"
---

涉及到两个表的查询 count 
表album：aid，aname，smallpic （aid为主键）

表photo：aid，pid，photopath……（aid为外键，pid为主键）

现在需要取到最新的三个album，且要得到每个album有多少张photo：

select top 3 album.aid, album.aname,album.smallpic, A.amount from album join ( select aid,count(*) as amount from photo group by aid)A
on album.aid=A.aid