0

--Edit

I still dont get it for some reason. I will give more detail about my problem.

jobs table

   id, title ... all details
    1, title1,...
    2, title2,...
    3, title3,...
    4, title4,...

job_user table

id,id_job,id_user
1,1,1
2,2,3
3,3,3
4,4,4

following_job table

id,id_job,id_user
1, 1, 3

So basically, user 3 has 2 jobs (2,3), and he follows job 1 of user 1. so, if i login as user 3, i would like to get all details jobs of user <> 3 (just the requirement that i need to do). i would get the result

id,id_job,id_user
    1,1,1 
    4,4,4

My goal results would be :

id,title..., following_id
        1,title1,...,1
        4,title4,...,0

the following_id will be added as result above, since user 3 followed id_job 1 so its following_id = 1 else = 0. And id_job 1,4 will joined with jobs table to get details about it : title ...

I am doing the follow/unfollow job functionality

Thanks all

5
  • 1
    Do you have a table which contains the job IDs for which you want to search? Commented Apr 20, 2017 at 6:10
  • 1
    Why is it id 2 for id_job 3 in your results? The table associates id 1 with it. Commented Apr 20, 2017 at 6:14
  • 1
    what do you want in the first column ? row number or what ? Commented Apr 20, 2017 at 6:14
  • 1
    Your id column in your result is nonsensical. That aside, consider handling the logic of missing values in application code (e.g. PHP), if that's available. Commented Apr 20, 2017 at 6:14
  • Thank you guys for quick answer. i edited my question including my full query Commented Apr 20, 2017 at 6:17

1 Answer 1

3

Typically you would have a second table which contains the id_job values you want to search/match, something like this:

has_jobs

id_job | (other columns...)
2
3

Then, you could just left join has_jobs to following_job to get the result you want:

SELECT t2.id
       t1.id_job,
       CASE WHEN t2.id_job IS NULL THEN 0 ELSE 1 END AS following_id
FROM has_jobs t1
LEFT JOIN following_job t2
    ON t1.id_job = t2.id_job

If you don't have an actual has_jobs table, then you could use an inline table instead:

SELECT t2.id
       t1.id_job,
       CASE WHEN t2.id_job IS NULL THEN 0 ELSE 1 END AS following_id
FROM
(
    SELECT 2 AS id_job
    UNION ALL
    SELECT 3
    -- add more IDs here if wanted
) t1
LEFT JOIN following_job t2
    ON t1.id_job = t2.id_job
Sign up to request clarification or add additional context in comments.

1 Comment

Biegeleisn : Thanks for your answer. I have been trying but no luck. Can you please take a look my post that i edit. Thanks much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.