SQL实践遇到的知识点

聚集函数count()

  • count()统计元组的个数,即行数
  • count(0)、count(1)与count(*)的执行效率是一样的
  • count(column)与count(*)
    • 如果column中含null,count(column)不会统计null,count(*)会统计null;
    • 如果column确定不为null,则count(row)与count(*)都会走索引,执行效率一样;
    • 如果含where条件表达式,count()比count(row)快;因此尽量使用count();
    • 如果经常count(*)的话则可以找一个最小的column建立非聚集索引以避免全表扫描而影响整体性能;

连接函数concat

  • concat()用于将两个字符串连接为一个字符串,例如表示出席率连接%:CONCAT( ROUND( ( count( tml.checkin_date ) / COUNT( tml.course_id ) * 100 ), 2 ), "%" ) AS attendentRate
  • group_concat()用于连接同一行的字符串,与group by一起使用。
    sql SELECT (SELECT GROUP_CONCAT( cdp1.dept_name, ":", cdp1.count_participant ORDER BY cdp1.dept_name ) FROM count_dept_people cdp1 WHERE cdp1.course_id = tcl.id )as namelist FROM tml group by tml.id

猜你喜欢

转载自www.cnblogs.com/pycrab/p/10635849.html