加入两个MySQL表的结果

时间:2016-10-13 10:34:38

标签: mysql join inner-join

我有两个MySQL表,我无法从中获得结果。

第一个只是公司名单及其名称:

companies:
____________________________
companyid  |  companyname  |
1               comp1
2               comp2

第二个是用户角色列表:

roles:
_____________________________________________________
roleid  |  uid  | role  |  companyid  |  suspended  |
1           1     dir           1           0
2           1     manag         2           0

我想得到一个看起来像的结果:

_______________________
companyname  |  role  |
comp1           dir
comp2           manag

我的查询下面没有返回任何内容......

SELECT companies.companyid, roles.role FROM companies INNER JOIN roles ON roles.companyid=companies.companyname where (uid = 1 and suspended = 0)

2 个答案:

答案 0 :(得分:1)

您正在加入roles.companyid = companies.companyname,这是不正确的,因为companyname是一个字符串,而不是一个整数。

请改为尝试:

SELECT companies.companyid, roles.role FROM companies INNER JOIN roles ON roles.companyid=companies.companyid where (uid = 1 and suspended <> 0);

答案 1 :(得分:1)

检查您的加入条件。

试试这个,

c.execute("UDPATE X SET A = ?, C = ?, D = D + 1 WHERE B = ?", [newA, newC, oldB])
if c.rowcount == 0:
    c.execute("INSERT INTO X(A,B,C,D) VALUES (?,?,?,?)", [...])