this is my program , and i want to show 0 when server no data
select
a.toolid ,
a.chamber,
b.datetime,
b.value
from “table1” a
left join “table2”b
on a.toolid = b.toolid
where b.datetime between ‘2022-06-01’ and now()
i test isnull and zn, but all can't use
The restrictions on the table2
table in the WHERE
clause should be moved to the ON
clause of the join. Then, use COALESCE
to display a default value for those table2
columns whose values are not present.
SELECT
a.toolid,
a.chamber,
b.datetime, -- can use COALESCE here too, see below
COALESCE(b.value, 0) AS value
FROM table1 a
LEFT JOIN table2 b
ON a.toolid = b.toolid AND
b.datetime BETWEEN '2022-06-01' AND NOW();
Note that we could also use COALESCE
on b.datetime
, but then we would need to display a default date. If we wanted instead to display default text, then we would have to first cast b.datetime
to text.
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.