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