看看这个sql语句的写法是否有问题

6 views
Skip to first unread message

wanglei

unread,
Jul 13, 2009, 10:52:30 PM7/13/09
to cph...@googlegroups.com
$count_query = "select count(*) as `count` from `articles_category` where date_format(`post_time`,'%m') = '".$month_m."' and date_format(`post_time`,'%y') = '".($year_y)."'";

mysql数据库,where后面是否可以直接date_format()来格式化这个当前这个表的列值?还是要用 子SELECT语句?

Fwolf

unread,
Jul 14, 2009, 1:17:42 AM7/14/09
to cph...@googlegroups.com
1、可把时间条件转换成先后时间直接比大小?效率应该更高
2、count(*) --> count(1)

至于date_format这个函数有没有,查手册不就得了么?

On Tue, Jul 14, 2009 at 10:52:30AM +0800, wanglei wrote:
> $count_query = "select count(*) as `count` from `articles_category` where date_format(`post_time`,'%m') = '".$month_m."' and date_format(`post_time`,'%y') = '".($year_y)."'";
>
> mysql数据库,where后面是否可以直接date_format()来格式化这个当前这个表的列值?还是要用 子SELECT语句?
>

> --~--~---------~--~----~------------~-------~--~----~
> 您收到此信息是由于您订阅了 Google 论坛“CPHPUG-中国PHP用户组”论坛。
> 要在此论坛发帖,请发电子邮件到 cph...@googlegroups.com
> 要退订此论坛,请发邮件至 cphpug+un...@googlegroups.com
> 更多选项,请通过 http://groups.google.com/group/cphpug?hl=zh-CN 访问该论坛
> -~----------~----~----~----~------~----~------~--~---
>

--
http://www.fwolf.com/
读出下面的字,你将获得月薪2000000的工作,试题如下:簟璁醭歙艽绱癀穑魍旃偬彘硪钚鲥硐蓰。

signature.asc

wanglei

unread,
Jul 14, 2009, 2:22:18 AM7/14/09
to cph...@googlegroups.com
1,有道理,用date_format()的话可能要在mysql中转换每个'post_time',而先转换只需要在php中把条件转换一次。

Peak Jing

unread,
Jul 14, 2009, 11:26:52 AM7/14/09
to cph...@googlegroups.com
MySQL中是有 date_format函数的,您的使用方法也是对的,不过这样是无法命中索引的,推荐使用between 的方式,这样可以使用 post_time 字段的索引(如果有)。

select count(*) as ct from articles_category where post_time between START_DATE and END_DATE;

SQL_CALC_FOUND_ROWS是在 select .... limit M,N 时使用的关键字,使用此关键字后可以通过 found_rows() 来得到本次查询结果集中的记录总数,但就是由于可以得到这个非常有价值的数据,select 要款待相对多的工作,增加了 select 的查询时间,如果非完全命中索引的查询会大幅增加查询时间,一般在非确定是一个可行的高效率查询时,不推荐使用此关键字。具体如下:

select SQL_CALC_FOUND_ROWS * from articles_category where post_time between START_DATE and END_DATE limit 0,20;

select found_rows();

关于 count(1) 和 count(*) 查询效率问题,个人做过一些测试,感觉差别不大,但没有做过深入研究,还请高手们赐教。



2009/7/14 wanglei <bwan...@gmail.com>



--
Welcome to my website : http://peak.name/

wanglei

unread,
Jul 14, 2009, 8:00:58 PM7/14/09
to cph...@googlegroups.com

Peak Jing said the following on 07/14/2009 11:26 PM-91c3f19f-edw931b13-1619a674f3l-写道:
> MySQL中是有 date_format函数的,您的使用方法也是对的,不过这样是无法命中索引的,推荐使用between 的方式,这样可以使用
> post_time 字段的索引(如果有)。
> select count(*) as ct from articles_category where post_time between
> START_DATE and END_DATE;
>

哦,这个就是Fwolf说的方法,between是mysql专用的吗?

Fwolf

unread,
Jul 14, 2009, 8:46:31 PM7/14/09
to cph...@googlegroups.com

On Wed, Jul 15, 2009 at 08:00:58AM +0800, wanglei wrote:
>
> Peak Jing said the following on 07/14/2009 11:26 PM-91c3f19f-edw931b13-1619a674f3l-写道:
> > MySQL中是有 date_format函数的,您的使用方法也是对的,不过这样是无法命中索引的,推荐使用between 的方式,这样可以使用
> > post_time 字段的索引(如果有)。
> > select count(*) as ct from articles_category where post_time between
> > START_DATE and END_DATE;
> >
>
> 哦,这个就是Fwolf说的方法,between是mysql专用的吗?

我觉得用 between 和用 < > 实现是一样的,
sql优化应该能够达到差不多一致的效率。

>
> > SQL_CALC_FOUND_ROWS是在 select .... limit M,N 时使用的关键字,使用此关键字后可以通过 found_rows()
> > 来得到本次查询结果集中的记录总数,但就是由于可以得到这个非常有价值的数据,select 要款待相对多的工作,增加了 select
> > 的查询时间,如果非完全命中索引的查询会大幅增加查询时间,一般在非确定是一个可行的高效率查询时,不推荐使用此关键字。具体如下:
> >
> > select SQL_CALC_FOUND_ROWS * from articles_category where post_time between
> > START_DATE and END_DATE limit 0,20;
> >
> > select found_rows();
> >
> > 关于 count(1) 和 count(*) 查询效率问题,个人做过一些测试,感觉差别不大,但没有做过深入研究,还请高手们赐教。
> >
> >
> >
> > 2009/7/14 wanglei<bwan...@gmail.com>
> >
> >
> >
>

> --~--~---------~--~----~------------~-------~--~----~
> 您收到此信息是由于您订阅了 Google 论坛“CPHPUG-中国PHP用户组”论坛。
> 要在此论坛发帖,请发电子邮件到 cph...@googlegroups.com
> 要退订此论坛,请发邮件至 cphpug+un...@googlegroups.com
> 更多选项,请通过 http://groups.google.com/group/cphpug?hl=zh-CN 访问该论坛
> -~----------~----~----~----~------~----~------~--~---
>

--
http://www.fwolf.com/
Email Address: $ echo -n "ZndvbGZjbkBnbWFpbC5jb20K" | base64 -d

signature.asc
Reply all
Reply to author
Forward
0 new messages