求详细分析一下这段SQL语句


   
  $sql=mysql_query("select * from (select bookid,count(bookid) as degree from tb_borrow group by bookid) as borr join (select b.*,c.name as bookcasename,p.pubname,t.typename from tb_bookinfo b left join tb_bookcase c on b.bookcase=c.id join tb_publishing p on b.ISBN=p.ISBN join tb_booktype t on b.typeid=t.id where b.del=0) as book on borr.bookid=book.id order by borr.degree desc limit 10")
 

求详细分析一下这段SQL语句

   
  select *
  
from (
select bookid, count(bookid) as degree
from tb_borrow
group by bookid
) as borr
join (
select b.*,c.name as bookcasename,p.pubname,t.typename
from tb_bookinfo b
left join tb_bookcase c on b.bookcase=c.id
join tb_publishing p on b.ISBN=p.ISBN
join tb_booktype t on b.typeid=t.id
where b.del=0
) as book on borr.bookid=book.id
order by borr.degree desc limit 10

sql mysql

s155336 11 years, 10 months ago

这条语句其实获取的就是借调(tb_borrow )最多的10本书的所有所有信息。

下面的语句意思是:通过按组统计方式获取每本书的借调数(count(bookid))

   
  select bookid, count(bookid) as degree
  
from tb_borrow
group by bookid

下面语句的意思是:获取没有被删除的书的信息,包括主信息tb_bookinfo,tb_bookcase ,出版方 tb_publishing, 以及书本类型信息tb_booktype

   
  select b.*,c.name as bookcasename,p.pubname,t.typename
  
from tb_bookinfo b
left join tb_bookcase c on b.bookcase=c.id
join tb_publishing p on b.ISBN=p.ISBN
join tb_booktype t on b.typeid=t.id
where b.del=0

通过以上转换就成了

   
  select *
  
from (
书的bookid和书的借调数degree
) as borr
join (
书id和每本书的详细信息
) as book on borr.bookid=book.id
order by borr.degree desc limit 10

popopo answered 11 years, 10 months ago

Your Answer