値が入っているテーブルがあります
Table_hello
date col2
2012-01-31 23:01:01 a
2012-06-2 12:01:01 b
2012-06-3 20:01:01 c
日付を選択したい
- 3日以内の場合は日数
- 24 時間以内の場合は時間単位
- 60分前以下の場合は分単位
- 60 秒以内の場合は秒単位
- 3日以上前であれば簡易様式で
出力
for row1 2012-01-31 23:01:01
for row2 1 day ago
for row3 1 hour ago
更新私のSQLクエリ
select case
when TIMESTAMPDIFF(SECOND, `date`,current_timestamp) <= 60
then concat(TIMESTAMPDIFF(SECOND, `date`,current_timestamp), ' seconds')
when TIMESTAMPDIFF(DAY, `date`,current_timestamp) <= 3
then concat(TIMESTAMPDIFF(DAY, `date`,current_timestamp), ' days')end
when TIMESTAMPDIFF(HOUR, `date`,current_timestamp) <= 60
then concat(TIMESTAMPDIFF(HOUR, `date`,current_timestamp), ' hours')
when TIMESTAMPDIFF(MINUTE, `date`,current_timestamp) <= 60
then concat(TIMESTAMPDIFF(MINUTE, `date`,current_timestamp), ' minutes')
from table_hello
唯一の問題は、C ++のスイッチケースのようにSQLでブレークとデフォルトを使用できないことです