|
Выборка по дате
|
|||
---|---|---|---|
#18+
Hello! В таблице есть поле 'date' типа datetime (2001-10-10 03:04:22). 1. Как мне устроить выборку из таблицы строк со значением date, соответствующему ВЧЕРАШНЕМУ дню? 2. .....дню МЕСЯЧНОЙ давности? 3. Как по значению date определить соответствующий ей ДЕНЬ НЕДЕЛИ? Спасибо! ... |
|||
:
Нравится:
Не нравится:
|
|||
16.11.2001, 13:46 |
|
Выборка по дате
|
|||
---|---|---|---|
#18+
DAYOFWEEK(date) Returns the weekday index for date (1 = Sunday, 2 = Monday, ... 7 = Saturday). These index values correspond to the ODBC standard: mysql> select DAYOFWEEK('1998-02-03'); WEEKDAY(date) Returns the weekday index for date (0 = Monday, 1 = Tuesday, ... 6 = Sunday): mysql> select WEEKDAY('1997-10-04 22:23:00'); mysql> select WEEKDAY('1997-11-05'); DAYNAME(date) Returns the name of the weekday for date: mysql> select DAYNAME("1998-02-05"); DATE_ADD(date,INTERVAL expr type) DATE_SUB(date,INTERVAL expr type) ADDDATE(date,INTERVAL expr type) SUBDATE(date,INTERVAL expr type) These functions perform date arithmetic. They are new for MySQL Version 3.22. ADDDATE() and SUBDATE() are synonyms for DATE_ADD() and DATE_SUB(). In MySQL Version 3.23, you can use + and - instead of DATE_ADD() and DATE_SUB() if the expression on the right side is a date or datetime column. (See example) date is a DATETIME or DATE value specifying the starting date. expr is an expression specifying the interval value to be added or subtracted from the starting date. expr is a string; it may start with a `-' for negative intervals. type is a keyword indicating how the expression should be interpreted. The EXTRACT(type FROM date) function returns the 'type' interval from the date. The following table shows how the type and expr arguments are related: type value Expected expr format SECOND SECONDS MINUTE MINUTES HOUR HOURS DAY DAYS MONTH MONTHS YEAR YEARS MINUTE_SECOND "MINUTES:SECONDS" HOUR_MINUTE "HOURS:MINUTES" DAY_HOUR "DAYS HOURS" YEAR_MONTH "YEARS-MONTHS" HOUR_SECOND "HOURS:MINUTES:SECONDS" DAY_MINUTE "DAYS HOURS:MINUTES" DAY_SECOND "DAYS HOURS:MINUTES:SECONDS" MySQL allows any punctuation delimiter in the expr format. Those shown in the table are the suggested delimiters. If the date argument is a DATE value and your calculations involve only YEAR, MONTH, and DAY parts (that is, no time parts), the result is a DATE value. Otherwise the result is a DATETIME value: mysql> SELECT "1997-12-31 23:59:59" + INTERVAL 1 SECOND; -> 1998-01-01 00:00:00 mysql> SELECT INTERVAL 1 DAY + "1997-12-31"; -> 1998-01-01 mysql> SELECT "1998-01-01" - INTERVAL 1 SECOND; -> 1997-12-31 23:59:59 mysql> SELECT DATE_ADD("1997-12-31 23:59:59", INTERVAL 1 SECOND); -> 1998-01-01 00:00:00 mysql> SELECT DATE_ADD("1997-12-31 23:59:59", INTERVAL 1 DAY); -> 1998-01-01 23:59:59 mysql> SELECT DATE_ADD("1997-12-31 23:59:59", INTERVAL "1:1" MINUTE_SECOND); -> 1998-01-01 00:01:00 mysql> SELECT DATE_SUB("1998-01-01 00:00:00", INTERVAL "1 1:1:1" DAY_SECOND); -> 1997-12-30 22:58:59 mysql> SELECT DATE_ADD("1998-01-01 00:00:00", INTERVAL "-1 10" DAY_HOUR); -> 1997-12-30 14:00:00 mysql> SELECT DATE_SUB("1998-01-02", INTERVAL 31 DAY); -> 1997-12-02 mysql> SELECT EXTRACT(YEAR FROM "1999-07-02"); -> 1999 mysql> SELECT EXTRACT(YEAR_MONTH FROM "1999-07-02 01:02:03"); -> 199907 mysql> SELECT EXTRACT(DAY_MINUTE FROM "1999-07-02 01:02:03"); -> 20102 If you specify an interval value that is too short (does not include all the interval parts that would be expected from the type keyword), MySQL assumes you have left out the leftmost parts of the interval value. For example, if you specify a type of DAY_SECOND, the value of expr is expected to have days, hours, minutes, and seconds parts. If you specify a value like "1:10", MySQL assumes that the days and hours parts are missing and the value represents minutes and seconds. In other words, "1:10" DAY_SECOND is interpreted in such a way that it is equivalent to "1:10" MINUTE_SECOND. This is analogous to the way that MySQL interprets TIME values as representing elapsed time rather than as time of day. Note that if you add or subtract a date value against something that contains a time part, the date value will be automatically converted to a datetime value: mysql> select date_add("1999-01-01", interval 1 day); -> 1999-01-02 mysql> select date_add("1999-01-01", interval 1 hour); -> 1999-01-01 01:00:00 If you use really incorrect dates, the result is NULL. If you add MONTH, YEAR_MONTH, or YEAR and the resulting date has a day that is larger than the maximum day for the new month, the day is adjusted to the maximum days in the new month: mysql> select DATE_ADD('1998-01-30', Interval 1 month); -> 1998-02-28 Note from the preceding example that the word INTERVAL and the type keyword are not case sensitive. ... |
|||
:
Нравится:
Не нравится:
|
|||
05.12.2001, 11:21 |
|
|
start [/forum/topic.php?fid=47&fpage=717&tid=1856411]: |
0ms |
get settings: |
9ms |
get forum list: |
15ms |
check forum access: |
4ms |
check topic access: |
4ms |
track hit: |
31ms |
get topic data: |
11ms |
get forum data: |
2ms |
get page messages: |
77ms |
get tp. blocked users: |
2ms |
others: | 265ms |
total: | 420ms |
0 / 0 |