Przetwarzanie daty Unix w MySQL

Jeśli przechowujemy datę w formacie unixowym, czyli ilość sekund od początku epoki unixowej, rozwiązaniem przetwarzania takich danych mogą być zapytania, jak podano poniżej:

$result_week_day=mysql_query(“SELECT dayname(from_unixtime(datetime))
as week_day, COUNT(id) as how_many_times
FROM www_articles
GROUP BY week_day
ORDER BY DAYOFWEEK(from_unixtime(datetime)) ASC”)

$result_hours=mysql_query(“SELECT hour(from_unixtime(datetime)) as
hours, COUNT(id) as how_many_times
FROM www_articles
GROUP BY hours
ORDER BY hours ASC”)
or die (mysql_error());

$result_months=mysql_query(“SELECT
DATE_FORMAT(from_unixtime(datetime), ‘%Y %m’) as months, COUNT(id) as
how_many_times
FROM www_articles
GROUP BY months
ORDER BY months ASC”)
or die (mysql_error());

 

 

Pokaż, w które dni tygodnia najczęściej publikowane są artykuły, ile zostało opublikowanych i posegreguj je zaczynając od niedzieli:

$result_week=mysql_query(“SELECT dayname(from_unixtime(datetime)) as weeks, COUNT(dayname(from_unixtime(datetime))) as how_many_times
FROM www_articles
GROUP BY weeks
ORDER BY DAYOFWEEK(from_unixtime(datetime)) ASC”)
or die (mysql_error());

while ($get_week=mysql_fetch_array($result_week))
{
echo $get_week[‘weeks’] . ” (” . $get_week[‘how_many_times’] . “)<br>”;
}

warunek: odczytaj rekord, ktory w kolumnie datetime (czas unixowy) zawiera poniedziałki:
WHERE dayname(from_unixtime(datetime))=’Monday’

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA
Change the CAPTCHA codeSpeak the CAPTCHA code
 

This site uses Akismet to reduce spam. Learn how your comment data is processed.