我需要创建一个视图,包含 读者ID,读者姓名, 借阅数量。 用到的两个表如下~:
CREATE TABLE beanreader (
readerid varchar(20) NOT NULL,
readerName varchar(50) NOT NULL,
readerTypeId int NOT NULL,
lendBookLimitted int NOT NULL,
createDate datetime NOT NULL,
creatorUserId varchar NOT NULL,
removeDate datetime DEFAULT NULL,
removerUserId varchar DEFAULT NULL,
stopDate datetime DEFAULT NULL,
stopUserId varchar DEFAULT NULL,
PRIMARY KEY (readerid)
)
CREATE TABLE beanbooklendrecord (
id int NOT NULL ,
readerid varchar(20) NOT NULL,
bookBarcode varchar(20) NOT NULL,
lendDate datetime NOT NULL,
returnDate datetime,
lendOperUserid varchar(20) NOT NULL,
returnOperUserid varchar(20),
penalSum float DEFAULT 0,
PRIMARY KEY (id)
)
那个借阅数量,不知道怎么计算。。。
------解决方案--------------------
select a.readerid,a.readerName,count(*) as 借阅数量
from beanreader a join beanbooklendrecord b on a.readerid=b.readerid
group by a.readerid,a.readerName