2016-08-24 75 views
0

我正在使用CodeIgniter和MySQL。我已經job_post表,看起來象下面這樣:MySQL加入查詢不起作用

enter image description here

在這裏,我有一個像這樣寫查詢:

select jp.*,u.first_name,u.last_name,group_concat(DISTINCT s.skill) as sk,group_concat(DISTINCT c.name) as ct,ufj.fav_id,ufj.is_favourite 
from job_post as jp 
left join industry as ind on ind.ind_id = jp.industry_id 
left join city c ON(FIND_IN_SET(c.city_id, jp.city) > 0) 
left join skill s ON(FIND_IN_SET(s.skill_id, jp.skill) > 0) 
join users as u on u.user_id = jp.emp_id 
left join user_favourite_job as ufj on ufj.job_id = jp.job_id and ufj.user_id = 8 
where jp.city in (2) and jp.is_delete = 1 group by job_id 

當我有關城ID爲1的jp.city in (1)比它給人完美的結果,但是當我在jp.city in (2)通過城市ID爲2。

那麼我的查詢應該改變什麼?

注意:當我通過城市id 2比它顯示第一個和最後一個記錄。但現在它不起作用。

+0

可能是ID不一致PLZ檢查你的IDS – Karthi

+0

使用jp.city LIKE「%2%」作爲不會給正確的結果 –

+1

節省逗號分隔不好用一對多的關係 –

回答

1

使用c.city_id而不是在where子句中使用jp.city。

select jp.*,u.first_name,u.last_name,group_concat(DISTINCT s.skill) as sk,group_concat(DISTINCT c.name) as ct,ufj.fav_id,ufj.is_favourite 
from job_post as jp 
left join industry as ind on ind.ind_id = jp.industry_id 
left join city c ON(FIND_IN_SET(c.city_id, jp.city) > 0) 
left join skill s ON(FIND_IN_SET(s.skill_id, jp.skill) > 0) 
join users as u on u.user_id = jp.emp_id 
left join user_favourite_job as ufj on ufj.job_id = jp.job_id and ufj.user_id = 8 
where c.city_id in (2) and jp.is_delete = 1 group by job_id 
1

更新where子句

where jp.city like '%2%' and jp.is_delete = 1 

,或者如果它包含1,2和11,12下方使用等

where jp.city like '%1%2%' and jp.city not like '%11%12%' and jp.is_delete = 1 

但使用它不是最優的,所以請保持的ID爲一對多relationshiop