我在构建SQL查询时遇到问题。假设我有一个表专家;例如:
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 6 | Ahmed | Mouloud |
| 7 | Alice | Lanouix |
+----+------------+-----------+
表expert_competence,允许加入专家的能力:
+----+---------------+-----------+
| id | competence_id | expert_id |
+----+---------------+-----------+
| 28 | 1 | 7 |
| 29 | 3 | 7 |
| 30 | 4 | 7 |
| 31 | 5 | 7 |
| 32 | 19 | 7 |
| 33 | 20 | 7 |
| 34 | 1 | 6 |
| 35 | 5 | 6 |
| 36 | 6 | 6 |
| 37 | 21 | 6 |
+----+---------------+-----------+
和表能力:
+----+-------------------------------------------------------------------------+
| id | name |
+----+-------------------------------------------------------------------------+
| 1 | INSPECTEUR OPERATIONS AERIENNES |
| 2 | INSPECTEUR OPERATIONS AERIENNES SAC MONACO |
| 3 | INSPECTEUR OPERATIONS AERIENNES DCA MAURICE |
| 4 | CONSULTING AUPRES D'UNE CAA
... |
+----+-------------------------------------------------------------------------+
如何让所有掌握能力为1和3(非OR)的专家?
我尝试了这个但是它没有工作(找到掌握能力1和3的所有专家):
SELECT DISTINCT ex.*
FROM expert ex
LEFT JOIN expert_competence ec
ON ec.expert_id = ex.id
WHERE ec.competence_id = 1
AND ec.competence_id = 3
我也尝试过这个查询:
SELECT DISTINCT ex.*
FROM expert ex
LEFT JOIN expert_competence ec
ON ec.expert_id = ex.id
WHERE ec.competence_id IN (1,3);
结果:
+----+-----------+------------+
| id | last_name | first_name |
+----+-----------+------------+
| 7 | Lanouix | Alice |
| 6 | Mouloud | Ahmed |
+----+-----------+------------+
预期结果:
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 7 | Alice | Lanouix |
+----+------------+-----------+
答案 0 :(得分:0)
请改为尝试:
SELECT DISTINCT
ex.*
FROM
expert AS ex
LEFT JOIN expert_competence AS ec ON ec.expert_id = ex.id
WHERE
ec.competence.id IN (1, 8)
通过使用IN
,您可以为列指定多个可能的条件。
答案 1 :(得分:0)
SELECT DISTINCT ex.*
FROM expert_competence ec
LEFT JOIN expert ex
ON ec.expert_id = ex.id
WHERE ec.competence.id IN (1, 8)
刚刚注意到ec.competence.id
它是否真的以表中的名字命名?现在我在查询中修改了一些条件,并在JOIN
查询中修改了一些表格。
让我知道它是否适合你。
答案 2 :(得分:0)
根据您提供的输入,您不清楚自己的目标是什么,我假设您正在寻找类似的内容,我认为您已经{{{I} 1}}表存储所有主ID。
competence_master
答案 3 :(得分:0)
试试这个例子:
<Path x:Name="ButtonPath"
Margin="1"
Stroke="Gray"
StrokeThickness="1"
StrokeStartLineCap="Square"
StrokeEndLineCap="Square"
Stretch="Uniform"
VerticalAlignment="Center"
HorizontalAlignment="Center"
>
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigure StartPoint="0,100">
<LineSegment Point="0,-100"/>
</PathFigure>
<PathFigure StartPoint="100,0">
<LineSegment Point="-100,0"/>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
答案 4 :(得分:0)
最后,我建立了一个有效的解决方案:
SELECT ex.* FROM
expert ex LEFT JOIN expert_competence ec
ON ec.expert_id = ex.id
WHERE ec.competence_id WHERE ec.competence_id = 1
AND ex.id in (SELECT ex.id FROM expert ex
LEFT JOIN expert_competence ec
ON ec.expert_id = ex.id
WHERE ec.competence_id = 3);