1. 程式人生 > >mysql 兩個時間比較 其中一個可能為null 解決辦法

mysql 兩個時間比較 其中一個可能為null 解決辦法

mysql 兩個時間比較 其中一個可能為null



SELECT * FROM (
SELECT qi.id,qci.id comment_id,qi.question_time,
if(qi.question_time>IFNULL(qci.comment_time,0),qi.question_time,qci.comment_time) newest_time
from question_info qi
LEFT JOIN question_comment_info qci on qci.question_id = qi.id
/* 使用者頭像 */
LEFT JOIN user_info u ON u.id = qi.user_id  AND u.disable_flag = 0 
LEFT JOIN user_info uu on uu.id = qci.user_id AND uu.disable_flag = 0 
/* 語音資訊 */
LEFT JOIN record_info rci on rci.comment_id = qci.id 
/* 訂單資訊,判斷偷聽 */
LEFT JOIN user_balance_payment ubp on ubp.payment_program_id = qci.id and ubp.type_id=6 and ubp.user_id = 3491 
ORDER BY newest_time desc
) a

第一重null為0

第二重比較排序

第三重比較排序

經過多重比較

終於成功地把 mysql 兩個時間比較 其中一個可能為null 排序成功解決

mysql 複雜多個欄位時間排序,時間欄位有null

SELECT * from (
SELECT qi.id question_id,qi.question_content,qi.question_desc,qi.type_id ,qi.comment_num
        question_comment_num,
        u.user_name ,qi.user_id,qi.question_time,
        qi.question_view_num,qi.invited_person_id,qi.is_anonymous, u.pic_name user_pic,u.head_img_url user_headimgurl,
        qci.id commentedId,qci.comment_content,qci.user_id comment_user_id,qci.comment_num,qci.good_num
        ,commentu.user_name commenter_name,commentu.head_img_url commenter_headimgurl,commentu.pic_name
        commenter_picname ,
        rci.record_name,rci.duration,
				if(TIMESTAMPDIFF( MINUTE, qi.question_time, now() )>3*24*60,1,0) time_out
        ,case when qgi_one.read_flag >=0 then 1 else 0 end good_flag
        ,ubp.id ubp_id,if(TIMESTAMPDIFF( MINUTE,qci.comment_time, now())>60*3,1,0) comment_time_out,
if(qi.question_time>IFNULL(qci.comment_time,0),qi.question_time,qci.comment_time) newest_time
from question_info qi
LEFT JOIN question_comment_info qci on qci.question_id = qi.id
/* 使用者頭像 */
LEFT JOIN user_info u ON u.id = qi.user_id  AND u.disable_flag = 0 
LEFT JOIN user_info commentu on commentu.id = qci.user_id AND commentu.disable_flag = 0 
/* 語音資訊 */
LEFT JOIN record_info rci on rci.comment_id = qci.id 
/* 訂單資訊,判斷偷聽 */
LEFT JOIN user_balance_payment ubp on ubp.payment_program_id = qci.id and ubp.type_id=6  and ubp.status_id =2 and ubp.user_id = 3491 
/* 點贊系統,判斷是否已經點贊成功 */
LEFT JOIN question_goods_info qgi_one on qgi_one.comment_id = qci.id and qgi_one.user_id = 3491
WHERE qi.id not in (
	select qi.id question_id
	from question_info qi 
	LEFT JOIN question_comment_info qci on qi.id = qci.question_id 
	LEFT JOIN record_info rci on rci.comment_id =qci.id 
	LEFT JOIN question_goods_info qgi_one on qgi_one.comment_id = qci.id and qgi_one.user_id = 3491
	LEFT JOIN user_balance_payment ubp on ubp.payment_program_id = qi.id and ubp.type_id=6  and ubp.status_id =2 and ubp.user_id = 3491 
	where qi.disable_flag = 0 AND is_anonymous = 1 and invited_person_id is not NULL and record_name is NULL   and (qi.user_id= 3488 or qci.user_id= 3488)
)
and is_anonymous = 1 and qi.disable_flag = 0   and (qi.user_id= 3488 or qci.user_id= 3488)
 group by newest_time 
UNION
SELECT  qi.id question_id,qi.question_content,qi.question_desc,qi.type_id ,qi.comment_num
        question_comment_num,
        u.user_name ,qi.user_id,qi.question_time newest_time,
        qi.question_view_num,IFNULL(qi.invited_person_id,100),qi.is_anonymous, u.pic_name user_pic,u.head_img_url user_headimgurl,
        qci.id commentedId,qci.comment_content,qci.user_id comment_user_id,qci.comment_num,qci.good_num
        ,commentu.user_name commenter_name,commentu.head_img_url commenter_headimgurl,commentu.pic_name
        commenter_picname ,
        rci.record_name,rci.duration,
				if(TIMESTAMPDIFF( MINUTE, qi.question_time, now() )>3*24*60,1,0) time_out
        ,case when qgi_one.read_flag >=0 then 1 else 0 end good_flag
        ,ubp.id ubp_id,if(TIMESTAMPDIFF( MINUTE,qci.comment_time, now())>60*3,1,0) comment_time_out,
				qi.question_time newest_time
from question_info qi
LEFT JOIN question_comment_info qci on qci.question_id = qi.id
/* 使用者頭像 */
LEFT JOIN user_info u ON u.id = qi.user_id  AND u.disable_flag = 0 
LEFT JOIN user_info commentu on commentu.id = qci.user_id AND commentu.disable_flag = 0 
/* 語音資訊 */
LEFT JOIN record_info rci on rci.comment_id = qci.id 
/* 訂單資訊,判斷偷聽 */
LEFT JOIN user_balance_payment ubp on ubp.payment_program_id = qci.id and ubp.type_id=6  and ubp.status_id =2 and ubp.user_id = 3491 
/* 點贊系統,判斷是否已經點贊成功 */
LEFT JOIN question_goods_info qgi_one on qgi_one.comment_id = qci.id and qgi_one.user_id = 3491
WHERE qi.id not in (
	select qi.id question_id
	from question_info qi 
	LEFT JOIN question_comment_info qci on qi.id = qci.question_id 
	LEFT JOIN record_info rci on rci.comment_id =qci.id 
	LEFT JOIN question_goods_info qgi_one on qgi_one.comment_id = qci.id and qgi_one.user_id = 3491
	LEFT JOIN user_balance_payment ubp on ubp.payment_program_id = qi.id and ubp.type_id=6  and ubp.status_id =2 and ubp.user_id = 3491 
	where qi.disable_flag = 0 AND is_anonymous = 1 and invited_person_id is not NULL and record_name is NULL   and (qi.user_id= 3488 or qci.user_id= 3488)
)
and is_anonymous = 1 and qi.disable_flag = 0   and qi.user_id=qci.user_id and qi.user_id=3488
 group by newest_time 
) b 
ORDER BY newest_time desc


相關推薦

mysql 時間比較 其中一個可能null 解決辦法

mysql 兩個時間比較 其中一個可能為null SELECT * FROM ( SELECT qi.id,qci.id comment_id,qi.question_time, if(qi.question_time>IFNULL(qci.comment_time

java8 時間比較

比如在15:30:30之前: LocalTime.now().isBefore(LocalTime.of(15, 30,30)) 或15:30:30之後 LocalTime.now().isAfter(LocalTime.of(15, 30,30)) 相等 LocalTim

Mysql 欄位合成一個欄位顯示

在工作中,有時會產生需要合併兩個欄位的情況使用下面都可以: CONCAT(str1,str2,...) CONCAT_WS(separator,str1,str2,...) 1、CONCAT(str1,str2,...) SELECT floorNo,unit,CON

一個類有方法,其中一個是同步的,另一個是非同步的; 現在又執行緒A和B,請問:當執行緒A訪問此類的同步方法時,執行緒B是否能訪問此類的非同步方法?

一個類有兩個方法,其中一個是同步的,另一個是非同步的;現在又兩個執行緒A和B,請問:當執行緒A訪問此類的同步方法時,執行緒B是否能訪問此類的非同步方法? 答案:可以 驗證 package com.my.test2; public class ClassA { public syn

php 實戰之array_merge函式特性-其中一個引數null

最近做專案時,需要合併兩個從資料庫中讀取的陣列,於是想到了用php中的array_merge函式,最後結果總是為null,通過dump,發現這是由於其中的一個數組是null造成的。演示如下: <?php $arr1 =null; $arr2 = array('tom'

同張表中同時查詢字段顯示一個字段,對字段進行按時間排序

principal mount sel con AC code rom inter nbsp select b.bid_name as bidName,bd.repayment_way as depict,r.exact_repayment_time as time, r

比較時間字符串的大小

缺點 com 使用 就是 -s 大小 能夠 lan 參與 String a="2003-10-12"; String b="2003-10-21";int result=a.compareTo(b);if (result==0)out.print("a=b");else

比較時間的大小 舉例:CompareDate("12:00","11:15")

ret fun col urn hour spl 兩個 大小 eth //比較兩個時間的大小 舉例:CompareDate("12:00","11:15") function CompareDate(t1, t2) { var date = new Date();

mysql查詢獲得時間的時間差

1.獲得當前時間和另一時間的時間差 SELECT TIMESTAMPDIFF(SECOND,(select last_locationTime from municipal_employee where employee_id=9),now()) as diffTime; mysql的 T

MySQL的date資料型別轉換問題及時間時間間隔的時間

由於更換資料庫的原因,MySQL遇到了各種坑。 在MySQL資料庫裡建表表欄位為COLLECT_TIME     TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 實際上該欄位是date。很奇怪, 然後在java裡面獲取該欄位值居然是英文的時間戳

計算時間戳之間的天數的一種比較高效的演算法

因為在做hadoop計算的時候需要按照時間來分片,所以需要對long型的時間戳進行Partition,本來想了一下兩個時間先歸零到當天的0點,然後想減除以24小時就是中間的天數,這個演算法沒錯,但是我沒有考慮到時區的問題,歸零的演算法是(t1-t1%h24)t是時間戳,h表示

Android 比較時間的大小,時間差,格式化時間,以及格式化時間大小差值、格式化秒錶

一、比較時間的大小-三種方式: 1、獲取時間戳比較 timestamp1 > timestamp2 2、轉換為Date比較 val date=Date() val date2=時間2 date.after(date2) 3、使用工具類(推薦) val dat

lua比較時間點,返回相差幾天

-- 上一個時間 local lastYear = os.date("%Y", 1490889600) local lastMonth = os.date("%m", 1490889600) local lastDay = os.date("%d",

MySQL中l獲取時間的年、月、日、小時、分鐘、秒之差

MySQL中l獲取兩個時間的年之差: select timestampdiff(year, "2017-12-25 19:15:16","2018-12-25 23:55:16") as year_diff; +-----------+ | year_diff | +----------

LUA 比較時間點(os.date())之間的時間間隔值

--[[比較兩個時間,返回相差多少時間]] function timediff(long_time,short_time) local n_short_time,n_long_time,carry,

【jQuery日期處理】時間大小的比較

function checkEndTime(){ var startTime=$("#startTime").val(); var start=new Date(startTime.replace

MySql查詢時間點之間的日期列表

統計資料時 , 會經常按月份或者天數來彙總資料 ; 但如果某幾個月份或天數沒有資料的時候 , 我們仍需返回一個空值 , 方法如下 : 1. 建立輔助數值序號表 DROP TABLE IF

PHP比較時間相差的天數

        $time1 = strtotime('2018-06-01 10:19:20'); //當前時間          $time2 = strtotime('2018-05-31 10:

比較時間字串的大小

現在有兩個字串,是從表單讀取來的日期性,“2003-10-12”與“2003-10-21”, 現在如何判斷他們的大小。 如果不想再轉為date型別比較 有一種簡單的方法 String a="2003-10-12"; String b="2003-10-21"; int re

【python】將列表合併字典,其中一個列表Key,一個列表Value

先說函式:dict(zip(列表1,列表2)) 列表1作為key,列表2作為value 下面看一下程式碼: #定義兩個列表 list1 = range(0,10) list2 = range(10,20) #合併為字典,呼叫dict(zip()) dict_name