Adamhumbug Posted March 12, 2021 Share Posted March 12, 2021 Hi All, I have the following, and when i run it the ifnull() is returning null rather than 0 as shown in the attached. Any help on this would be greatly appreciated. select * from ( SELECT ptsl_ptd_id, SUBSTRING(ptsl_date,1,10) as ptsl_date, ptsl_z_id, z_rfid, ptsl_limit FROM `prs_ptsl` inner join prs_z on ptsl_z_id=z_id where ptsl_ptd_id='7' ) as limits left join ( SELECT pr_ptd_id, za_sdate, za_z_id, za_z_rfid, IFNULL(count(za_pr_id), 0) as used FROM `prs_za` inner join prs_pr on za_pr_id=pr_id where prs_pr.pr_status = 'Approved' or prs_pr.pr_status = 'Submitted' group by za_sdate, za_z_id, za_z_rfid ) as used on limits.ptsl_ptd_id=used.pr_ptd_id and limits.ptsl_date=used.za_sdate and ptsl_z_id=za_z_id where ptsl_date = '2021-06-12' and (ptsl_limit - IFNULL(used, 0) >= 0) limit 100 Quote Link to comment Share on other sites More sharing options...
Barand Posted March 12, 2021 Share Posted March 12, 2021 I can't see your data so I don't know which rows match, but if you have a LEFT JOIN, and there is no match, then the used.* will be NULL, regardless of your uses of IFNULL() in the subquery. You need the IFNULL() in the top level select (instead of "SELECT * ") to fix. Quote Link to comment Share on other sites More sharing options...
Adamhumbug Posted March 12, 2021 Author Share Posted March 12, 2021 Thanks so much for your reply as always! Will the IFNULL be used in the maths at the bottom of this query? (ptsl_limit - IFNULL(used, 0) Quote Link to comment Share on other sites More sharing options...
Barand Posted March 12, 2021 Share Posted March 12, 2021 The "WHERE ptsl_date = '2021-06-12' " is OK in the WHERE clause but you cannot put conditions on a LEFT JOINed subquery (or table) in a WHERE clause, they need to be in the join's ON clause. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.