Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Hitung Jangka Waktu di MySQL

Kueri ini akan menampilkan hari, jam, menit, dan detik yang tepat dari 1 Februari 2011 Tengah malam:

SELECT 
    TRIM(REPLACE(CONCAT(
        IF(dy=0,'',IF(dy=1,'1 day ',CONCAT(dy,' days '))),
        IF(hr=0,'',IF(hr=1,'1 hr ', CONCAT(hr,' hrs  '))),
        IF(mn=0,'',IF(mn=1,'1 min ',CONCAT(mn,' mins '))),
        IF(sc=0,'',IF(sc=1,'1 sec ',CONCAT(sc,' secs ')))),'  ',' '))
    TimeDisplay
FROM (SELECT dy,hr,mn,MOD(sec_aaaa,60) sc
FROM (SELECT dy,hr,FLOOR((sec_aaa - dy*86400 - hr*3600)/60) mn,sec_aaa sec_aaaa
FROM (SELECT dy,FLOOR((sec_aa - (dy*86400))/3600) hr,sec_aa sec_aaa
FROM (SELECT FLOOR(sec_a/86400) dy,sec_a sec_aa
FROM (SELECT (UNIX_TIMESTAMP() - UNIX_TIMESTAMP('2011-02-01 00:00:00')) sec_a)
A) AA) AAA) AAAA) B;

Ganti saja '2011-02-01 00:00:00' dengan nilai tanggal waktu atau nama kolom tabel yang Anda inginkan.

Cobalah !!!

PERBARUI 2011-10-06 13:38 EDT

Saya menulis fungsi tersimpan yang dapat Anda panggil yang akan menangani ini untuk Anda:

DELIMITER $$

DROP FUNCTION IF EXISTS `test`.`GetTimeDisplay` $$
CREATE FUNCTION `test`.`GetTimeDisplay` (GivenTimestamp TIMESTAMP)
RETURNS VARCHAR(32)
DETERMINISTIC
BEGIN

    DECLARE rv VARCHAR(32);
    DECLARE diff BIGINT;

    SET diff = UNIX_TIMESTAMP() - UNIX_TIMESTAMP(GivenTimestamp);
    SELECT
        TRIM(REPLACE(CONCAT(
            IF(dy=0,'',IF(dy=1,'1 day ',CONCAT(dy,' days '))),
            IF(hr=0,'',IF(hr=1,'1 hr ', CONCAT(hr,' hrs  '))),
            IF(mn=0,'',IF(mn=1,'1 min ',CONCAT(mn,' mins '))),
            IF(sc=0,'',IF(sc=1,'1 sec ',CONCAT(sc,' secs ')))),'  ',' '))
    INTO rv
    FROM (SELECT dy,hr,mn,MOD(sec_aaaa,60) sc
    FROM (SELECT dy,hr,FLOOR((sec_aaa - dy*86400 - hr*3600)/60) mn,sec_aaa sec_aaaa
    FROM (SELECT dy,FLOOR((sec_aa - (dy*86400))/3600) hr,sec_aa sec_aaa
    FROM (SELECT FLOOR(sec_a/86400) dy,sec_a sec_aa
    FROM (SELECT ABS(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(GivenTimestamp)) sec_a)
    A) AA) AAA) AAAA) B;

    IF diff = 0 THEN
        SET rv = '0 secs';
    END IF;
    IF diff < 0 THEN
        SET rv = CONCAT(rv,' From Now');
    END IF;
    IF diff > 0 THEN
        SET rv = CONCAT(rv,' Ago');
    END IF;

    RETURN rv;

END $$

DELIMITER ;

Anda dapat menulis ulang kueri seperti ini:

$query = "select country, rprice as regPrice, mprice as midPrice, pprice as prePrice, saddress as streetAddress,
                _id as ID, lat, lng, sname as Name, logo, admin_level_1 as state, locale as city, test.GetTimeDisplay(rdate) as regDate, 
                test.GetTimeDisplay(mdate) as midDate, test.GetTimeDisplay(pdate) as preDate, 
                format((acos(sin(radians($lat1)) * sin(radians(lat)) + cos(radians($lat1)) *
                cos(radians(lat)) * cos(radians($lng1) - radians(lng))) * 6378),1) as distance from stationDetails where 
                (acos(sin(radians($lat1)) * sin(radians(lat)) + cos(radians($lat1)) * cos(radians(lat)) * 
                cos(radians($lng1) - radians(lng))) * 6378) <= $rad order by $sort asc, $type asc";

Anda mungkin ingin memindahkan fungsi yang disimpan ke database lain. Kode yang saya miliki menempatkan fungsi yang disimpan dalam database pengujian.

Cobalah !!!




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Pembuat Kueri Laravel DI MANA TIDAK

  2. Parameter @Nama tidak ditemukan dalam koleksi

  3. dua kunci asing, cara memetakan dengan laravel fasih

  4. Peringatan:mysqli_connect():Server MySQL telah hilang

  5. Jalankan pemicu prosedur tersimpan di SqlFiddle. Mysql