0

In my query, can i give some default value to subquery?

One of my subquery might not have any value.. in that case it returns null as whole result. Can I assign some default value if even one of subquery is null?

select 
    t1 . *, t2.speed, t3.Accelerometerx
from
    (SELECT 
        *
    FROM
        `table1`
    ) as t1,
    (SELECT 
       *
    FROM
        table1)
    as t2
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
  • possible duplicate of [MySql Query Replace NULL with Empty String in Select](http://stackoverflow.com/questions/9560723/mysql-query-replace-null-with-empty-string-in-select) – Jenn Jul 14 '14 at 18:01

3 Answers3

0

Check the nvl function, it should solve your problem.

user3722371
  • 145
  • 6
0

USE CASE/IF:

CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE

EXAMPLE, instead of:

where
    `vehicle_tripdetails`.`Param_ID` = '131'
        and trip_id = 127) as t1,
(SELECT 
    time_stamp, `vehicle_tripdetails`.`Param_Data` as speed
FROM
    `vehicle_tripdetails`
where
    `vehicle_tripdetails`.`Param_ID` = '13'
        and trip_id = 127) as t2,

You can use:

where
    `vehicle_tripdetails`.`Param_ID` = '131'
        and trip_id = 127) as t1,
CASE 
  WHEN (SELECT  time_stamp, `vehicle_tripdetails`.`Param_Data` as speed FROM
    `vehicle_tripdetails` where
    `vehicle_tripdetails`.`Param_ID` = '13'
        and trip_id = 127) 
  IS NOT NULL 
THEN (SELECT  time_stamp, `vehicle_tripdetails`.`Param_Data` as speed FROM
    `vehicle_tripdetails` where
    `vehicle_tripdetails`.`Param_ID` = '13'
        and trip_id = 127) 
ELSE
   'default value'
END CASE
AS t2
Andrzej Reduta
  • 767
  • 1
  • 7
  • 15
0

Use ISNull in your select clause as below

select    t1.*,
          CASE ISNULL(t2.speed) WHEN 1 THEN 0 ELSE t2.speed END,
          CASE ISNULL(t3.xyz) WHEN 1 THEN 0 ELSE t3.xyz END

Here the default value is 0. This will work if you don't select all records using *

Anil Soman
  • 2,443
  • 7
  • 40
  • 64