我必须使用表格job_postings
和job_applies
。如何获得用户尚未申请的所有工作?
以下是我的job_postings
表的列:
id, user_id, title, description, duties, salary, child_count, benefits, created_at, updated_at
以下是job_applies
表
user_id, posting_id, status, created_at, updated_at
我尝试了什么:
$job_postings = DB::table('job_postings')
->select(
'job_postings.title',
'job_postings.description',
'job_postings.duties',
'job_postings.salary',
'job_postings.child_count',
'job_postings.benefits',
'job_postings.created_at',
'job_postings.id AS posting_id',
'job_postings.user_id')
->join('job_applies', 'job_applies.posting_id', '!=', 'job_postings.id')
->where('job_applies.user_id', "=" , user()->id)
->get();
答案 0 :(得分:2)
在Mysql中你应该写(用你的字段列表替换*)。你可以适应你的语言
SELECT *
FROM JOB_POSTING A
LEFT JOIN JOB_APPLIES B ON B.POSTING_ID = A.ID AND B.USER_ID = A.USER_ID
WHERE B.POSTING_ID IS NULL
或
SELECT *
FROM JOB_POSTING A
WHERE NOT EXISTS (SELECT 1 FROM JOB_APPLIES B WHERE B.POSTING_ID = A.ID AND B.USER_ID = A.USER_ID)