count() 统计总记录数 比如100条:
select count(*) from user where 1=1
select
*
from
shipmentlist,shipmentscrib
where
(shipmentlist.shipmentlistno=shipmentscrib.shipmentlistno)
and (year(shipmentlist.shipmentdate)=year(now()))
and (month(shipmentlist.shipmentdate)=month(now())
or month(shipmentlist.shipmentdate)=month(now())-1 )
ORDER BY shipmentdate DESC
格式化了一下你的SQL,分析一下.
那么上面的条件.
将变为
其实,对于 查询 当月和上一月
相当于
shipmentlist.shipmentdate = 上月的1号
AND shipmentlist.shipmentdate 下月的1号
LAST_DAY(NOW()) 可以获取 本月的最后一天.
DATE_ADD( LAST_DAY(NOW()) INTERVAL 1 DAY ) 可以获取下月第一天.
最后 SQL 修改为:
AND shipmentlist.shipmentdate DATE_ADD( LAST_DAY(NOW()) INTERVAL 1 DAY )
第二个写法出来的是相同id值的计数情况.一般来说,一条select语句的执行顺序如下:from子句→where→group by(having)→select→order by→limit,因为聚集函数是在select中的,所以是在执行完group by之后再执行count()函数,所以第二条写法是得不到总的记录数的.如果你要获得总的记录数就用第一种写法.
myisam直接count然后limit取.innodb count记得用二级索引作为条件.count(*)from table where uid0 . 这样.不然不准还慢.
应该是这样的::
select name,brand_id,concat(floor((sum(goods_num)/(select sum(goods_num) from tablename))*100),'%') as goods_num_rate
from tablename group by name;
以上就是土嘎嘎小编为大家整理的mysql怎么查比率相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!