MySQL连接查询无效

时间:2016-08-24 08:11:35

标签: mysql codeigniter

我正在使用CodeIgniter和MySQL。我有一个job_post表,如下所示:

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 

当我在jp.city in (1)将城市ID传递为1时,它会给出完美的结果,但是当我在jp.city in (2)将城市ID传递为2时。

那么我应该在查询中更改什么?

注意:当我通过城市ID 2而不是显示第一个和最后一个记录时。但是现在它没有用。

2 个答案:

答案 0 :(得分:1)

更新where子句

<android.support.v7.widget.Toolbar
            app:navigationIcon="?homeAsUpIndicator"
            ...

或如果它包含1,2和11,12等,则使用下面的

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

但使用它并不是最佳的,所以请将id作为一对多关系

答案 1 :(得分:1)

使用c.city_id而不是在子句中使用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