Reputation: 4523
I am trying to manage query using IF ELSE Condition. But instead of IF ELSE I was to use cases.
I want to GROUP BY by taking decision on User type
Here is my query.
SELECT *
FROM users
WHERE flag = 1 AND blockStatus = 0
GROUP BY
CASE userType
WHEN 1 THEN
jobId
WHEN 2 THEN
experienceId
END
Somehow it is fetching multiple records of same jobId or experienceId.
What is the right way to implement this kind of condition?
SQL FIDDLE HERE :
http://sqlfiddle.com/#!2/e3df6/1
Upvotes: 0
Views: 1758
Reputation: 3417
You can try like this..
SELECT *
FROM user
where usertype =1 group by jobid
union
SELECT *
FROM user
where usertype =2 group by experienceId
Upvotes: 1
Reputation: 3272
If you are using php to create this query then why not create your query based on the data in php:-
$query = ($userType==1) ? "Select * from users where flag=1 and blockstatus=0 group by jobId" : "Select * from users where flag=1 and blockstatus=0 group by experienceId";
Upvotes: 1