加入多个表格

时间:2016-08-18 19:03:22

标签: mysql sql

我有2个SQL表:

select * from operation;
id |        name
----+--------------------
1 | Account add
2 | Account edit

 select * from role_operation;
 id |   role   | operation
----+----------+-----------
  1 | admin    |        1
  2 | operator |        1
  3 | admin    |        2

操作表中的id列和role_operation中的operation列已关联。 (外键)。如何获取操作name = Account add

的所有行

我尝试了SELECT role_operation.role, operation.name FROM role_operation INNER JOIN operation ON role_operation.operation=operation.id;

但是这给了我所有在role_operation表的操作字段中都有操作ID的行。

2 个答案:

答案 0 :(得分:4)

使用

SELECT role_operation.role, operation.name 
FROM role_operation 
INNER JOIN operation ON role_operation.operation=operation.id
where  operation.name = 'Account add';

答案 1 :(得分:2)

SELECT role_operation.role, operation.name FROM role_operation
INNER JOIN operation on operation.id = role_operation.operation
WHERE operation.name='Account add'