I need a small help with a SQL query.
I have two tables: tbltrans
and tbltrans_temp
. I want to select the maximum of tbltrans_temp max(tbltrans_temp.transid)
.
If tbltrans_temp
is empty and it returns null, it should then take the max of tbltrans.transid
.
If both tables are empty, it should just return 0.
I tried the following but didn't get the result expected.
select ifnull(ifnull(max(t1.transid), max(t2.transid)), 0)
from tbltrans_temp t1
left join tbltrans as t2
This works using COALESCE
:
select coalesce(maxtemptrans, maxtrans, 0)
from (select max(transid) maxtemptrans from tbltrans_temp) t,
(select max(transid) maxtrans from tbltrans ) t2
Try:
select coalesce((select max(transid) from tbltrans_temp),
(select max(transid) from tbltrans),
0)
You were close -- just remove the join:
select ifnull(ifnull(max(t1.transid), max(t2.transid)),0)
from tbltrans_temp t1, tbltrans t2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.