'Syntax error in case statement while creating MySQL view
I am getting mysql #1064 error while trying to create a view
CREATE VIEW trip_summary AS select `driver_details`.`DriverId`, CONCAT(driver_details.Firstname, ' ', driver_details.Lastname, ' ', driver_details.TaxiPlateNo) AS TaxiDriver, count(taxi_trip.AutoId) AS TotalTrip, GROUP_CONCAT(taxi_trip.AutoId) AS TripIds, sum(taxi_trip.TripDistance) AS TotalTripDistance, sum(taxi_trip.TotalFare) AS TotalTripFare from `driver_details` left join `taxi_trip` on (`taxi_trip`.`DriverId` = `driver_details`.`DriverId` and `taxi_trip`.`PickupLAT` != 0 and `taxi_trip`.`DropLAT` != 0 and `taxi_trip`.`TotalFare` != 0) where taxi_trip.AutoId!=0 and
CASE WHEN `DriverIdFun`()!='' THEN taxi_trip.DriverId = `DriverIdFun`()
END AS field1,
CASE WHEN `From_Date`()!='' THEN taxi_trip.RequestDate >= `From_Date`()
END AS field2,
CASE WHEN `To_Date`()!='' THEN taxi_trip.RequestDate <= `To_Date`()
END AS field3 group by taxi_trip.DriverId
Solution 1:[1]
This isn't a complete answer, but may contain enough info to get you started....
Typically (but not everytime), a VIEW into a set of database tables does not have a WHERE clause, and in your case the WHERE clause looks like a dog's breakfast.
I'd start with this much simpler view and develop from here....
CREATE VIEW trip_summary AS
SELECT
dd.`DriverId`,
CONCAT(dd.Firstname, ' ', dd.Lastname, ' ', dd.TaxiPlateNo) AS TaxiDriver,
count(tt.AutoId) AS TotalTrip,
GROUP_CONCAT(tt.AutoId) AS TripIds,
sum(tt.TripDistance) AS TotalTripDistance,
sum(tt.TotalFare) AS TotalTripFare
FROM `driver_details` AS dd
LEFT JOIN `taxi_trip` AS tt
ON tt.`DriverId` = dd.`DriverId`
AND tt.`PickupLAT` != 0
AND tt.`DropLAT` != 0
AND tt.`TotalFare` != 0
GROUP BY tt.DriverId
Then when you query the view, you select what you do and don't want out of the view by means of the where clause
SELECT * FROM trip_summary WHERE DriverID=1234;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |