>  기사  >  데이터 베이스  >  mysql에서 월을 쿼리하는 방법

mysql에서 월을 쿼리하는 방법

藏色散人
藏色散人원래의
2021-11-26 14:04:4115068검색

mysql에서 월을 쿼리하는 방법: 1. "select date_format(DATE_SUB(curdate(), INTERVAL 0 MONTH), '%m')"을 통해 이번 달을 쿼리합니다. 2. "INTERVAL 1 MONTH를 통해 이전 달을 쿼리합니다. ".

mysql에서 월을 쿼리하는 방법

이 기사의 운영 환경: Windows 7 시스템, mysql 5.0, Dell G3.

mysql에서 월을 쿼리하는 방법은 무엇입니까?

mysql 월 데이터를 쿼리합니다.

//查看本月数据
SELECT
*
FROM
content_publish
WHERE
date_format(publish_time, '%Y %m') = date_format(DATE_SUB(curdate(), INTERVAL 0 MONTH),'%Y %m')
 
//查看上个月数据
SELECT
*
FROM
content_publish
WHERE
date_format(publish_time, '%Y %m') = date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y %m')
 
//查询上上个月数据
SELECT
*
FROM
content_publish
WHERE
date_format(publish_time, '%Y %m') = date_format(DATE_SUB(curdate(), INTERVAL 2 MONTH),'%Y %m')
 
//查询当前月份
select date_format(DATE_SUB(curdate(), INTERVAL 0 MONTH), '%m')
//查询上个月月份
select date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH), '%m')
//查询上上个月月份
select date_format(DATE_SUB(curdate(), INTERVAL 0 MONTH), '%m')

오늘

select * from 表名 where to_days(时间字段名) = to_days(now());

어제

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1

지난 7일

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)

거의 30 days

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

이번 달

SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, &#39;%Y%m&#39; ) = DATE_FORMAT( CURDATE( ) , &#39;%Y%m&#39; )

이전 달

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , &#39;%Y%m&#39; ) , date_format( 时间字段名, &#39;%Y%m&#39; ) ) =1

이번 분기 데이터 조회

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());

지난 분기 데이터 조회

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

올해 조회' s data

select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());

작년 쿼리 data

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

이번주 데이터 조회

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,&#39;%Y-%m-%d&#39;)) = YEARWEEK(now());

지난주 데이터 조회

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,&#39;%Y-%m-%d&#39;)) = YEARWEEK(now())-1;

지난 달 데이터 조회

select name,submittime from enterprise where date_format(submittime,&#39;%Y-%m&#39;)=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),&#39;%Y-%m&#39;)
select * from user where DATE_FORMAT(pudate,&#39;%Y%m&#39;) = DATE_FORMAT(CURDATE(),&#39;%Y%m&#39;) ; 
select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,&#39;%y-%m-%d&#39;)) = WEEKOFYEAR(now()) 
select * from user where MONTH(FROM_UNIXTIME(pudate,&#39;%y-%m-%d&#39;)) = MONTH(now()) 
select * from user where YEAR(FROM_UNIXTIME(pudate,&#39;%y-%m-%d&#39;)) = YEAR(now()) and MONTH(FROM_UNIXTIME(pudate,&#39;%y-%m-%d&#39;)) = MONTH(now()) 
select * from user where pudate between  上月最后一天  and 下月第一天

이번 달 데이터 조회

select name,submittime from enterprise   where date_format(submittime,&#39;%Y-%m&#39;)=date_format(now(),&#39;%Y-%m&#39;)

지금으로부터 6개월 후의 쿼리 월간 데이터

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

추천 학습: "mysql 비디오 튜토리얼"

위 내용은 mysql에서 월을 쿼리하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.