如何使用SQL获得此问题的答案?
什么是守门人的名字,谁在鼠标的门口?
(表格格式不是最好,但想象它们有3列,而“ID ...”的行是标题)
整个“看门人”表
ID gatekeeper gate
1 TE 4
2 ER 1
3 PU 2
4 WD 3
5 TR 5
整个“门”表
ID animal gate
1 cat 3
2 dog 1
3 mouse 2
是这样的吗?
SELECT gatekeeper
FROM gatekeeper
WHERE gate = (SELECT gate from gates where animal = mouse);
答案 0 :(得分:3)
SELECT gatekeeper.gatekeeper
FROM gatekeeper
JOIN gates on gates.gate = gatekeeper.gate AND gates.animal = 'mouse'
答案 1 :(得分:1)
你如何定义'easy'?最简单的方法应该是你最舒服的方式。但是,子选择确实会降低查询的性能。最好使用连接,如下所示:
SELECT gk.gatekeeper
FROM gatekeeper AS gk
JOIN gate AS g on g.gate = gk.gate
WHERE g.animal = 'mouse'