어제 날짜를 선택하는 MySQL
날짜가 어제 인 값을 어떻게 표시하고 계산할 수 있습니까? 나는 time()
데이터베이스에 날짜를 삽입했습니다. 예:
URL: google.com youtube.com google.com youtube.com test.com youtube.com
DateVisited: 1313668492 1313668540 1313668571 13154314
표에 여러 개의 URL이있는 URL과 어제 방문한 URL의 수도 표시하고 싶습니다. 결과 예 :
LINK | timesExisted | timesVisitedYesterday
Google.com | 2 | 2
youtube.com| 3 | 3
나는 이미 어제 날짜를 얻는 것에 대한 아이디어를 가지고 있지만 어제 URL이 몇 번이나 존재했는지와 URL이 테이블에 몇 번 존재했는지를 세는 아이디어가 없습니다.
어제 날짜를 얻는 가장 간단하고 가장 좋은 방법은 다음과 같습니다.
subdate(current_date, 1)
쿼리는 다음과 같습니다.
SELECT
url as LINK,
count(*) as timesExisted,
sum(DateVisited between UNIX_TIMESTAMP(subdate(current_date, 1)) and
UNIX_TIMESTAMP(current_date)) as timesVisitedYesterday
FROM mytable
GROUP BY 1
호기심을 불러 일으키는 조건을 충족하는 행 sum(condition)
의 수 를 제공 하는 이유 는 성가신 case
문장이 필요하지만 mysql에서 부울 값은 1
true와 0
false에 대한 것이므로 조건을 합산하면 몇 번이나 효과적으로 계산됩니다. 사실입니다. 이 패턴을 사용하면 SQL 코드를 정리할 수 있습니다.
SELECT SUBDATE(NOW(),1);
now () 함수는 Timestamp에서 시스템의 현재 날짜와 시간을 되돌립니다.
당신이 사용할 수있는:
SELECT SUBDATE(CURDATE(),1)
당신이 사용할 수있는:
SELECT SUBDATE(NOW(), 1);
또는
SELECT SUBDATE(NOW(), INTERVAL 1 DAY);
또는
SELECT NOW() - INTERVAL 1 DAY;
또는
SELECT DATE_SUB(NOW(), INTERVAL 1 DAY);
표현식을 사용하여 어제 날짜를 얻을 수 있습니다 CAST(NOW() - INTERVAL 1 DAY AS DATE)
. 따라서 다음과 같이 작동 할 수 있습니다.
SELECT * FROM your_table
WHERE DateVisited >= UNIX_TIMESTAMP(CAST(NOW() - INTERVAL 1 DAY AS DATE))
AND DateVisited <= UNIX_TIMESTAMP(CAST(NOW() AS DATE));
지난주에 대한 쿼리 :
SELECT *
FROM dual
WHERE search_date BETWEEN SUBDATE(CURDATE(), 7) AND CURDATE()
선택한 답변이 정확하고 간결하지만 다른 답변에서 언급 한 구조에 대해 주장합니다.
SELECT * FROM your_table
WHERE UNIX_TIMESTAMP(DateVisited) >= UNIX_TIMESTAMP(CAST(NOW() - INTERVAL 1 DAY AS DATE))
AND UNIX_TIMESTAMP(DateVisited) <= UNIX_TIMESTAMP(CAST(NOW() AS DATE));
타임 스탬프없이 맨날 날짜 만 필요하다면 다음과 같이 작성할 수도 있습니다.
SELECT * FROM your_table
WHERE DateVisited >= CAST(NOW() - INTERVAL 1 DAY AS DATE)
AND DateVisited <= CAST(NOW() AS DATE);
The reason for using CAST
versus SUBDATE
is CAST
is ANSI SQL syntax. SUBDATE
is a MySQL specific implementation of the date arithmetic component of CAST
. Getting into the habit of using ANSI syntax can reduce headaches should you ever have to migrate to a different database. It's also good to be in the habit as a professional practice as you'll almost certainly work with other DBMS' in the future.
None of the major DBMS systems are fully ANSI compliant, but most of them implement the broad set of ANSI syntax whereas nearly none of them outside of MySQL and its descendants (MariaDB, Percona, etc) will implement MySQL-specific syntax.
Last or next date, week, month & year calculation. It might be helpful for anyone.
Current Date:
select curdate();
Yesterday:
select subdate(curdate(), 1)
Tomorrow:
select adddate(curdate(), 1)
Last 1 week:
select between subdate(curdate(), 7) and subdate(curdate(), 1)
Next 1 week:
between adddate(curdate(), 7) and adddate(curdate(), 1)
Last 1 month:
between subdate(curdate(), 30) and subdate(curdate(), 1)
Next 1 month:
between adddate(curdate(), 30) and adddate(curdate(), 1)
Current month:
subdate(curdate(),day(curdate())-1) and last_day(curdate());
Last 1 year:
between subdate(curdate(), 365) and subdate(curdate(), 1)
Next 1 year:
between adddate(curdate(), 365) and adddate(curdate(), 1)
I adapted one of the above answers from cdhowie as I could not get it to work. This seems to work for me. I suspect it's also possible to do this with the UNIX_TIMESTAMP function been used.
SELECT * FROM your_table
WHERE UNIX_TIMESTAMP(DateVisited) >= UNIX_TIMESTAMP(CAST(NOW() - INTERVAL 1 DAY AS DATE))
AND UNIX_TIMESTAMP(DateVisited) <= UNIX_TIMESTAMP(CAST(NOW() AS DATE));
참고URL : https://stackoverflow.com/questions/7146828/mysql-selecting-yesterdays-date
'development' 카테고리의 다른 글
Xcode 4로 앱 빌드-PNG 이미지에 대한 오류가 항상 표시됨 (0) | 2020.09.08 |
---|---|
터미널에서 웹 스톰을 여는 방법 (0) | 2020.09.08 |
글꼴 크기 늘리기 크롬 콘솔 (0) | 2020.09.08 |
NSMutableArray의 특정 인덱스에 개체 추가 (0) | 2020.09.08 |
PHP를 사용하여 문자열에서 마지막 쉼표를 어떻게 제거합니까? (0) | 2020.09.08 |