3 回答
TA贡献2011条经验 获得超2个赞
SQL应该是
SELECT
u.id,
r.id AS roleid,
u.type,
r.username
FROM users u
LEFT JOIN roles r ON r.id = u.role_id
ORDER BY u.id ASC
我省略了您在 sql 中定义的一些列,因为我根本看不到它们的意义。不仅如此,它们可能看起来模棱两可。
例如:
SELECT
users.id, // User ID
roles.id AS roleid, // Role ID
type, // User type
role_id, // Role ID (We already have it, no need for it again)
id, // What ID? This is ambiguous and will fail
username // User Username
TA贡献1785条经验 获得超8个赞
像这样试试
public function get_users()
{
$users_list = $this->dbh->query('SELECT u.id AS userid, r.id AS roleid, r.type, u.role_id, u.username FROM users as u LEFT JOIN roles as r ON u.role_id = r.id ORDER BY u.id ASC');
return $users_list;
}
TA贡献1788条经验 获得超4个赞
由于两个表都有 id 列,因此需要指定哪个表引用<table_name>.id
public function get_users()
{
$users_list = $this->dbh->query('SELECT users.id AS userid, roles.id AS roleid, type, role_id, username FROM users LEFT JOIN roles ON users.role_id = roles.id ORDER BY users.id ASC');
return $users_list;
}
- 3 回答
- 0 关注
- 123 浏览
添加回答
举报