我正在製作一個PHP腳本來顯示當前用戶屬於哪個用戶角色。 數據是在MySQL中,我想要的是顯示所有可用角色及其所有角色,顯示用戶所屬的位置。爲什麼這個mySQL LEFT JOIN不起作用?它可以是WHERE子句嗎?
這是我的角色表
CREATE TABLE roles(
roleID int unsigned not null auto_increment primary key,
nombreRol char(50) not null
)
這裏的角色是:
registered
pending
student
former-student
,這就是USUARIOS表角色表
CREATE TABLE rolesUsuarios (
rolesUsuariosID int unsigned not null auto_increment primary key,
userID int not null,
nombreRol char(50) not null
)
連接表
我試圖創建一個LEFT JOIN像這樣:
SELECT
roles.nombreRol,
rolesUsuarios.userID
FROM roles
LEFT JOIN rolesUsuarios
ON roles.nombreRol = rolesUsuarios.nombreRol
WHERE rolesUsuarios.userID = 183
(我此刻不關心安全性,只是檢查了查詢工作)
在這個例子中,用戶183具有註冊和學生。
預期的結果應該是這樣的:
nombreRol -------- userID
registered ------- 183
pending ---------- NULL
student ---------- 183
former-student --- NULL
但我得到這樣的結果,而不是:
nombreRol -------- userID
registered ------- 183
student ---------- 183
我如何可以改變連接? (我跟着例子here,它並在那裏工作,我認爲我已經按照正確的查詢邏輯)
'OR .... IS NULL' –
@ PM77-1 - Nope。 – GurV