Home Backend Development PHP Tutorial php如何查询某月或者某日有数据且输出(文章归档功能)

php如何查询某月或者某日有数据且输出(文章归档功能)

Jun 13, 2016 am 10:37 AM
count from

php怎么查询某月或者某日有数据且输出(文章归档功能)
有 articles表

id class title content pubtime
1 10 标题1 内容1 1342756599
2 11 标题2 内容2 1339392385
3 12 标题3 内容3 1339390661
4 10 标题4 内容4 1339139926
5 13 标题5 内容5 1339139892
6 16 标题6 内容6 1342756624

希望查询的结果是:
1.如2012年8月、2012年6月内有文章,输出一次 2012年8月、2012年6月的链接,最好能计算出汇总,有多少篇文章。
2.如2012年7月16日、2012年7月28日有文章 则倒序输出 2012年7月28日、2012年7月16日的链接,最好能计算出汇总,有多少篇文章。

不会写,给个思路也行啊。

------解决方案--------------------

SQL code
SELECT count(1) FROM `articles` WHERE `pubtime` > 6月1号0点时间戳 and `postdate` <font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨
SELECT count(1) FROM `articles` WHERE `pubtime` > 6月1号0点时间戳 and `postdate`

------解决方案--------------------
SQL code
>select count(1), date(pubtime) from articles group by date(pubtime);<br><font color="#e78608">------解决方案--------------------</font><br>先把几个点的时间戳取出来.如2012年8月、2012年6月。<br>然后在拼接sql查询,应该很容易的。 <br>别着急,你试试<br><font color="#e78608">------解决方案--------------------</font><br>1、<br>select FROM_UNIXTIME(pubtime, '%Y-%m') as pubtime, count(*) as cnt from articles group by FROM_UNIXTIME(pubtime, '%Y-%m')<br><br><font color="#e78608">------解决方案--------------------</font><br>这是每个月:<br>
Copy after login
SQL code
>select count(1), extract(year_month from pubtime) from articles  group by extract(year_month from pubtime);<br><font color="#e78608">------解决方案--------------------</font><br>有个问题,在sql中使用函数会影响sql的执行效率,被用字段的主键什么的,有时候也没效果
Copy after login
探讨

这是每个月:
SQL code
>select count(1), extract(year_month from pubtime) from articles group by extract(year_month from pubtime);

引用:

SQL code
>select count(1), date(pubtime) from article……

------解决方案--------------------
探讨
我怎么能一次性查出 2012年6月 和 2012年8月
一年有12个月 我不能每个月都写一个语句吧,我想一次性查询一个月每一天是否有文章 一个月30天呢
就是这儿 怎么做呢

------解决方案--------------------
SQL code
SELECTSUM(CASE when MONTH( FROM_UNIXTIME(pubtime, '%Y-%m-%d'))=6 THEN 1 ELSE 0 END)AS sum_6,SUM(CASE when MONTH( FROM_UNIXTIME(pubtime, '%Y-%m-%d'))=8 THEN 1 ELSE 0 END)AS sum_8FROM dc_admin<div class="clear">
                 
              
              
        
            </div>
Copy after login
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between counta and count The difference between counta and count Nov 20, 2023 am 10:01 AM

The Count function is used to count the number of numbers in a specified range. It ignores text, logical values, and null values, but counts empty cells. The Count function only counts the number of cells that contain actual numbers. The CountA function is used to count the number of non-empty cells in a specified range. It not only counts cells containing actual numbers, but also counts the number of non-empty cells containing text, logical values, and formulas.

You can't specify target table 'table_name' for update in FROM clause - How to solve MySQL error: Unable to update target table in FROM clause You can't specify target table 'table_name' for update in FROM clause - How to solve MySQL error: Unable to update target table in FROM clause Oct 05, 2023 am 10:25 AM

Hello, the following is an article within 1500 words, titled: Youcan'tspecifytargettable'table_name'forupdateinFROMclause-How to solve the MySQL error: Unable to update the target table in the FROM clause, specific code examples are needed. During the development of MySQL database, we sometimes encounter the following error message: Youcan'tspecify

Summary of usage differences of MySQL Count function Summary of usage differences of MySQL Count function Nov 29, 2022 pm 04:47 PM

This article introduces to you the usage differences of MySQL’s Count function. The need for statistical data is very easy to encounter in our daily development. MySQL also supports a variety of calculation functions. Next, let’s take a look at the differences between them. , and whether they have some pitfalls.

What is the usage of count function in php What is the usage of count function in php Sep 09, 2021 pm 05:08 PM

The usage of the count function in PHP is: [count(array,mode);], where the parameter array specifies the array to be counted, and the parameter mode specifies the mode of the function.

How to solve the problem of count distinct multiple columns in mysql How to solve the problem of count distinct multiple columns in mysql Jun 03, 2023 am 10:49 AM

The reproduced test database is as follows: CREATETABLE`test_distinct`(`id`int(11)NOTNULLAUTO_INCREMENT,`a`varchar(50)CHARACTERSETutf8DEFAULTNULL,`b`varchar(50)CHARACTERSETutf8DEFAULTNULL,PRIMARYKEY(`id`))ENGINE= InnoDBAUTO_INCREMENT=1DEFAULTCHARSET=latin1;The test data in the table is as follows. Now we need to count the deduplicated columns of these three columns.

Learn to use the PHP count() function to count the number of array elements Learn to use the PHP count() function to count the number of array elements Jun 27, 2023 am 10:37 AM

PHP is a very popular server-side programming language with powerful development capabilities, especially in array operations. Arrays in PHP are widely used to store and manage multiple data items. When operating on an array, it is often necessary to count the number of elements in the array. This can be achieved by using the count() function in PHP. The count() function is PHP's built-in function for counting the number of array elements. When using this function, you only need to pass the array as a parameter of the function

How does java Count count elements in a stream? How does java Count count elements in a stream? May 11, 2023 pm 04:07 PM

Note 1. Count is a terminal operation that can count the total number of elements in the stream. The return value is of long type. 2. count() returns the count of elements in the stream. This is a special case of induction (an induction operation takes a sequence of input elements and combines them into a summary result by repeatedly applying the combining operation). This is a terminal operation and may have consequences and side effects. After a terminal operation is performed, the pipe is considered consumed and cannot be reused. Example // Verify whether there is a string in the list starting with a, and match the first one, that is, return truebooleananyStartsWithA=stringCollection.stream().anyMatch((s

What is the use of the from tag in html What is the use of the from tag in html Sep 15, 2022 pm 05:36 PM

In HTML, the from tag is used to create an HTML form (form field) for user input to collect and transfer user information. All content in the form will be submitted to the server; the syntax "<form action="submit address" method="Submission method" name="Form name">Form control</form>". A form can contain one or more form elements, such as input, select, and textarea.

See all articles