MySQLi LEFT JOIN 查询抛出语法错误
MySQLi LEFT JOIN query is throwing a syntax error
我有两个 table 'lr_users' 和 'lr_ranks' 具有以下 table 结构:
lr_users:
user_id(pk), username, rank(fk), job_id(fk), date_joined
lr_ranks: rank_id(pk), name
$qry= mysqli_query($con, "SELECT lr_users.username, lr_users.rank, lr_users.job_id, lr_users.date_joined lr_ranks.name AS rankname FROM lr_users
LEFT JOIN lr_ranks ON lr_users.rank = lr_ranks.rank_id")or die(mysqli_error($con));
$rows = mysqli_num_rows($qry);
以上是我的 PHP 生成 'LEFT JOIN' 查询的代码,但是当我 运行 它时,我得到以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.name AS rankname FROM lr_users LEFT JOIN lr_ranks ON lr_users.rank = lr_ran' at line 1.
我做错了什么?
语法错误(lr_users.date_joined
前缺少逗号):
$qry= mysqli_query($con, "SELECT lr_users.username, lr_users.rank, lr_users.job_id, lr_users.date_joined, lr_ranks.name AS rankname FROM lr_users
LEFT JOIN lr_ranks ON lr_users.rank = lr_ranks.rank_id")or die(mysqli_error($con));
$rows = mysqli_num_rows($qry);
如果你的代码格式更清晰,就不会出现这种错误:
$query = "
SELECT u.username
, u.rank
, u.job_id
, u.date_joined
, r.name rankname
FROM lr_users u
LEFT
JOIN lr_ranks r
ON u.rank = r.rank_id;
";
我有两个 table 'lr_users' 和 'lr_ranks' 具有以下 table 结构:
lr_users: user_id(pk), username, rank(fk), job_id(fk), date_joined
lr_ranks: rank_id(pk), name
$qry= mysqli_query($con, "SELECT lr_users.username, lr_users.rank, lr_users.job_id, lr_users.date_joined lr_ranks.name AS rankname FROM lr_users
LEFT JOIN lr_ranks ON lr_users.rank = lr_ranks.rank_id")or die(mysqli_error($con));
$rows = mysqli_num_rows($qry);
以上是我的 PHP 生成 'LEFT JOIN' 查询的代码,但是当我 运行 它时,我得到以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.name AS rankname FROM lr_users LEFT JOIN lr_ranks ON lr_users.rank = lr_ran' at line 1.
我做错了什么?
语法错误(lr_users.date_joined
前缺少逗号):
$qry= mysqli_query($con, "SELECT lr_users.username, lr_users.rank, lr_users.job_id, lr_users.date_joined, lr_ranks.name AS rankname FROM lr_users
LEFT JOIN lr_ranks ON lr_users.rank = lr_ranks.rank_id")or die(mysqli_error($con));
$rows = mysqli_num_rows($qry);
如果你的代码格式更清晰,就不会出现这种错误:
$query = "
SELECT u.username
, u.rank
, u.job_id
, u.date_joined
, r.name rankname
FROM lr_users u
LEFT
JOIN lr_ranks r
ON u.rank = r.rank_id;
";