从表中选择具有多个条件的另一个表中的行

时间:2016-03-31 12:48:57

标签: mysql

我有3个表应用概念 app_to_concept

App表

app_id |    app_name

=================

1      |    foo
2      |    bar
3      |    foobar
4      |    barfoo

概念表

concept_id    |    concept_name
=========================
1              |    english
2              |    math
3              |    science
4              |    Fun

app_to_concept表

concept_id    |    app_id
=========================
1              |    1
2              |    1
3              |    2
4              |    3
2              |    2
2              |    4

我希望获得具有概念app listenglish的所有math(例如)

我怎样才能做到这一点?

修改

让我更具体一点

我有一些具有概念价值的复选框。当我选中复选框时,它返回与已检查概念相关的应用详细信现在我的案例是用例子解释

如果我选中了englishmath概念复选框,则会显示包含概念english math的应用详细信息,同样适用于{{ 1}}和science。 现在,当我选中funenglishmath时,结果应显示包含science english 的应用详细信息, math。意味着应用

scienceenglish

sciencemath

1 个答案:

答案 0 :(得分:0)

基本JOIN会这样做。

select a.app_name
from app_to_concept ac 
    INNER JOIN app_table a ON ac.app_id = a.app_id
    INNER JOIN concept_table c ON ac.concept_id = c.concept_id
WHERE c.concept_name IN ('english', 'math');