我试图将“ users”表中的“ userTypeId”与其在“ userRoles”表中对应的“ id”进行匹配,以便我也可以在“ userRoles”中回显“ roleName”。
表格如下:
userRoles
---------------------
id roleName
1 superuser
2 admin
3 editor
4 author
5 contributor
6 subscriber
users
---------------------
id userName userTypeId
1 heyjohnmurray 1
2 admin 2
我尝试了此查询,每当我检查它时,它都会回显“错误”
$roleQuery = "SELECT id, roleName, userTypeId FROM userRoles JOIN users ON id=userTypeId";
$roleResult = mysqli_query($dbconnect, $roleQuery);
if(!$roleResult){
echo 'WRONG';
} else {
echo 'RIGHT';
}
最佳答案
在表格上添加ALIAS
SELECT a.id, a.roleName, b.userTypeId
FROM userRoles a, users b
WHERE a.id = b.userTypeId
或更好地使用格式(ANSI SQL-92)
SELECT a.id, a.roleName, b.userTypeId
FROM userRoles a
INNER JOIN users b
ON a.id = b.userTypeId