skip to Main Content

My question i need Mysql Query if there is no value found in any table then i want to return the result value to zero else the value which i found.

i tried out every solution every solution which is provided by ChatGPT but i am failed to get my specific result

i tried out,

SELECT IF( EXISTS( SELECT RepairingId FROM expenses where WorkDate='2023-07-23 00:00:00' and VehRegNo='PB-56B-8508'), 1, 0) AS RepairId;` -- this is working but i want the value of RepairingId not just 1
         
SELECT IFNULL(RepairingId, 0) AS result FROM expenses where WorkDate='2023-07-24 00:00:00' and VehRegNo='PB-56B-8508';` -- this give me no value

SELECT CASE WHEN RepairingId IS NOT NULL THEN RepairingId ELSE 0 END AS resultFROM expenses where WorkDate='2023-07-24 00:00:00' and VehRegNo='PB-56B-8508'; -- this give me no value

2

Answers


  1. try

    SELECT IFNULL((SELECT RepairingId FROM expenses WHERE WorkDate='2023-07-23 00:00:00' AND VehRegNo='PB-56B-8508'), 0) AS RepairId;
    

    or

    SELECT IFNULL(RepairingId, 0) AS result FROM expenses WHERE WorkDate='2023-07-24 00:00:00' AND VehRegNo='PB-56B-8508' LIMIT 1;
    

    or

    SELECT CASE WHEN RepairingId IS NOT NULL THEN RepairingId ELSE 0 END AS result FROM expenses WHERE WorkDate='2023-07-24 00:00:00' AND VehRegNo='PB-56B-8508' LIMIT 1;
    
    Login or Signup to reply.
  2. Try this:

    SELECT IFNULL(
        (SELECT RepairingId FROM expenses WHERE WorkDate='2023-07-24 00:00:00' AND VehRegNo='PB-56B-8508' LIMIT 1),
        0
    ) AS result;
    

    You can see my example in dbfiddle

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search