Home > Backend Development > PHP Tutorial > mysql数据库循环语句该怎么写???

mysql数据库循环语句该怎么写???

WBOY
Release: 2016-06-23 14:16:44
Original
1302 people have browsed it

数据库结构

num   date
2     2013-07-24
5     2013-07-25
8     2013-08-03
10    2013-08-22
4     2013-09-10

最后要在页面中输出为一个行行统计格式
7月份  7条记录
8月份  18条记录
9月份  4条记录

要求sql语句只有一句话,有的人会想可以select (count(条件))
关键是还要换行的。。
有点不会了。


回复讨论(解决方案)

也不是说换行,数据库里是那样的结构,
意思就是只要你输入sql语句,执行
的结果就是我说的那样。全部列出来

被你弄糊涂了

select DATE_FORMAT(date, '%Y-%m') as date, count(*) as num from tbl_name group by 1
Copy after login
不行吗?
select DATE_FORMAT(date, '%Y-%m') as date, count(*) as num from (select 2 as num, '2013-07-24' as date  union select 5,'2013-07-25'  union select 8,     '2013-08-03'  union select 10,    '2013-08-22'  union select 4,     '2013-09-10') tgroup by 1
Copy after login
得到
Array
(
    [0] => Array
        (
            [date] => 2013-07
            [num] => 2
        )

    [1] => Array
        (
            [date] => 2013-08
            [num] => 2
        )

    [2] => Array
        (
            [date] => 2013-09
            [num] => 1
        )

)

被你弄糊涂了
我改改去,好像有点启发了


被你弄糊涂了
我改改去,好像有点启发了
select DATE_FORMAT(date, '%Y-%m') as date, count(*) as num from tbl_name group by 1

这个是有几条记录 没有计算每个月的和。貌似不对。我再研究看看

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template