php - Preventing multi-row returns while using INNER JOIN and IN clause: MySQL -
I have several tables that I am joining together to return to the search results page. In the search page, the user can toggle the criteria, and the page is dynamically reloaded based on the query results. In this regard, I am having a challenge with a special condition -
I have a table called jobs and there is another table for the job. One job can have one or more contract types. - Select the simple question
to join both of them in a.job_id, c.contract_type_name, from the job on an Inner Join Job_SiteC Job_id = c.job_id
This query works fine. However, if the user decides to toggle some contract types and chooses multiple contract types, then the query returns multiple rows with the same job ID. The query I am using is in the form of the following:
SELECT a.job_id, c.contract_type_name Job from a job JOB_Centract C on an INNER JOB. A.job_id = c.job_id and c.contract_type IN (1,2, 4)
So for the above code, if there are multiple contract type associations in a job then I get 3 lines is. Can I limit it to 1 line without a line - I'm worried about performance problems using DISTINCT
Thank you very much for your help in advance
If you do not need the name of the contract, you can do it with the existing
section :
Select j.job_id from j.job_id where it exists (select 1 from job_contract jc where j.job_id = jc.jobid and jc.contract_type IN (1,2,4)) ;
This eliminates / separate
by the group. This will perform best with an index at
job_contract (job_id, contract_type)
.
You can use the contract again:
select j Job_id, (from job_contract jc) 'j_job_id = jc.jobid and jc.contract_type IN (1, 2,4)) Job exists as a contract of j (choose 1 from job_contract jc, where j.job_id = jcjobid and jc.contract_type IN (1,2,4));
I recommend that you use group
/ different
due to the clarity of the query, as long as you have a good reason Do not
Comments
Post a Comment