1

我发现了同样的问题,按照步骤操作,但仍然出现错误。
我一直在寻找这个很多天。

我有 3 张桌子

用户

+----+-----------------------------+-------------+
| id | fname                       | lname       |
+----+-----------------------------+-------------+
| 4  | John                        | Menezes     |
+----+-----------------------------+-------------+

团体

+----+-----------------------------+-------------+
| group_id | group_name            | created_on  |
+----+-----------------------------+-------------+
| 1        | Facebook friends      | 25/12/2012  |
+----------+-----------------------+-------------+

小组成员

+----+-------------+-------------+
| id | user_id     | group_id    |
+----+-------------+-------------+
| 4  | 4           |     1       |
+----+-------------+-------------+

我想得到有点像这样的 最终输出

+----+--------------+-----------------------+
| fname |  lname    |        groupname      |
+----+--------------+-----------------------+
| John  | Menezes   |     Facebook Friends  |
+----+--------------+-----------------------+

我使用了这个查询

SELECT u.fname, u.lname, g.group_name
FROM user AS u
LEFT JOIN group_members AS gm ON gm.user_id = u.id
INNER JOIN group AS g ON g.group_id = gm.group_id

但它给了我这个错误

#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 4 行的 'group AS g ON g.group_id = gm.group_id LIMIT 0, 30' 附近使用正确的语法

也试过这个

where g.low_limit > 70
and g.high_limt < 120
4

2 回答 2

3

您遇到的问题是mysql http://dev.mysql.com/doc/refman/5.5/en/reserved-words.htmlgroup中的保留字。您需要将表名和列名放在反引号单引号中,以便 MySQL 将名称解释为表。

尝试

SELECT `u`.`fname`, `u`.`lname`, `g`.`group_name`
FROM `user` AS `u`
LEFT JOIN `group_members` AS `gm` ON `gm`.`user_id` = `u`.`id`
INNER JOIN `group` AS `g` ON `g`.`group_id` = `gm`.`group_id`

编辑:字符更改的名称。感谢@adrian

于 2013-04-01T20:24:29.250 回答
0

尝试:

SELECT u.fname, u.lname, g.group_name
FROM user AS u
LEFT JOIN group_members AS gm ON gm.user_id = u.id
INNER JOIN group AS g ON g.group_id = gm.id
于 2013-04-01T20:23:42.040 回答