来自同一引用表的多个sql连接

时间:2014-09-09 12:55:08

标签: mysql sql

我目前有以下查询:

select  title,location,categorie,division, 
    reference.description as CA 
from emploi 
left join reference on emploi.categorie = reference.code_ref 
where id_type = 'CA' and emploi.status ='1'

我有第二个查询,它是相同的但是从参考表中获取了不同的值:

select  title,location,categorie,division, 
    reference.description as DI 
from emploi 
left join reference on emploi.division = reference.code_ref 
where id_type = 'DI' and emploi.status ='1'

有没有办法执行这两个查询?

我以为这是下面的事情,但显然不起作用:

select  title,location,categorie,division, 
    reference.description as DI, reference.description as CA 
from emploi 
(left join reference on emploi.division = reference.code_ref 
    where id_type = 'DI')
(left join reference on emploi.categorie = reference.code_ref 
    where id_type = 'CA')
where emploi.status ='1'

谢谢!

注意:emploi表中的每个条目都有一个分区和类别列,它们都需要映射到相同行中的单独条目

编辑:
对于那些感兴趣的人来说,这是一个有效的解决方案:

select  title,location,categorie,division, 
    r.description as DI, r1.description as CA 
from emploi 
left join reference r on emploi.division = r.code_ref 
    and r.id_type = 'DI'
left join reference r1 on emploi.categorie = r1.code_ref 
    and r1.id_type = 'CA'
where emploi.status ='1'

4 个答案:

答案 0 :(得分:1)

你可以试试这个:

select  title,location,categorie,division, 
    r.description as DI, r1.description as CA 
from emploi 
left join reference r on emploi.division = r.code_ref 
left join reference r1 on emploi.categorie = r1.code_ref 
where emploi.status ='1'
and id_type in ('CA', 'DI')

目前,由于您where两次使用left join子句,因此语法不正确。同时将别名放到表中并使用这些别名来获取列名

这样的东西
select  tablename.title,tablename.location,tablename.categorie,tablename.division, 
    r.description as DI, r1.description as CA 
from emploi 
left join reference r on emploi.division = r.code_ref 
left join reference r1 on emploi.categorie = r1.code_ref 
where emploi.status ='1'
and id_type in ('CA', 'DI')

注意:使用正确的表格代替上面的tablename

修改: -

如果您不想使用IN子句,那么

select  title,location,categorie,division, 
    r.description as DI, r1.description as CA 
from emploi 
left join reference r on emploi.division = r.code_ref 
    and r.id_type = 'DI'
left join reference r1 on emploi.categorie = r1.code_ref 
    and r1.id_type = 'CA'
where emploi.status ='1'

假设id_typeemploi

的列

答案 1 :(得分:1)

有几种方法可以实现这一目标:

select  title,location,categorie,division, 
reference.description as DI 
from emploi 
left join reference on emploi.division = reference.code_ref 
where id_type IN ('DI','CA') and emploi.status ='1'

或者您可以将where子句更改为OR

where (id_type = 'DI' OR id_type = 'CA') and emploi.status ='1'

或者如果您期待您最初写的内容

select  title,location,categorie,division, 
reference.description as DI, reference.description as CA 
from emploi 
left join reference r1 on emploi.division = r1.code_ref 
and r1.id_type = 'DI'
left join reference r2 on emploi.categorie = r2.code_ref 
and r2.id_type = 'CA'

或者其他几种方式......

答案 2 :(得分:0)

它应该在没有方括号()的情况下工作,对表进行别名并在字段前加上别名。

答案 3 :(得分:0)

select  title,location,categorie,division, 
    reference.description as CA 
from emploi 
left join reference  reference on emploi.categorie = reference.code_ref 
left join reference reference1 on emploi.division = reference1.code_ref
where id_type in('CA','DI') and emploi.status ='1' order by id_type 

这对你有用,这里的order by用于定义select语句的顺序