【问题标题】:SQL SELECT JOIN multiple tablesSQL SELECT JOIN 多个表
【发布时间】:2021-03-27 12:50:27
【问题描述】:

我想选择所有没有特殊性或具有给定数组中的特殊性的约定。

约定

convention_id type
1c ty1
2c ty2
3c ty3

activity_type

activity_type_id effect_date motif fk_convention
1a xxxxx nothi 1c
2a xxxxx abc 1c
3a xxxxx def 2c
4a xxxxx poi 3c

activity_type_specialties

fk_activity_type fk_specialty
1a 1s
1a 2s
2a 3s
4a 3s

专业

speciality_id label
1s PEDIATRIE
2s GERIATRIE
3s NEPHROLOGIE

对于数组 ['1s'],查询必须返回约定 1c 和 2c(约定 1c 包含特长 1s 和 2s,约定 2c 没有特长)

【问题讨论】:

标签: sql join select left-join outer-join


【解决方案1】:

我不太明白这个问题,因为您的预期结果听起来不是第一个问题.. 但我希望能有所帮助:

select c.convention_id, c.type, a.*, at.*  from convention c
left join activity_type a on (a.fk_convention = c.convention_id)
left join activity_type_specialties at on (at.fk_activity_type = a.activity_type_id)
where at.fk_specialty is null

马丁

【讨论】:

    【解决方案2】:

    希望答案对你有帮助。

        select fk_specialty,fk_convertion from convertion c
        inner join activity_type a on c.convertion_id=a.fk_convertion
        inner join activity_type_specialties ats on 
        ats.fk_activity_type=a.activity_type_id
        inner join speciality s on s.speciality_id=ats.fk_specialty
        where fk_specialty in ('1c','2c')
    

    【讨论】:

    • 欢迎来到stackoverflow。如果你不完全理解这个问题,你最好问一两个问题,而不是提供答案。这是因为 stackoverflow 的其他用户会看到一个问题有答案(可能不正确),甚至可能不会尝试给出更好的答案。
    猜你喜欢
    • 2015-02-21
    • 1970-01-01
    • 2011-03-03
    • 2013-06-11
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    相关资源
    最近更新 更多