3

我是一个 MySQL 新手,无法弄清楚这里出了什么问题。我有两张桌子。左表称为锻炼。相关列是date(type date) 和id(type int)。

右表称为Workout_locations(相关字段:workout_id type int, and location_id type int)。

连接字段是Workouts.idWorkout_locations.workout_id

我想要做的就是得到一个包含两列的表格:(date来自锻炼)和location_id(来自Workout_locations)。我只需要根据几个字段从表中提取记录Workouts(sql 语句应该清楚地说明这一点)。

这是我的sql语法:

SELECT Workouts.date as date, Workout_locations.location_id as loc_id
FROM Workouts 
WHERE Workouts.pacegroup_id='9' AND (Workouts.date BETWEEN '2013-08-19' AND '2013-08-25') 
INNER JOIN Workout_locations ON Workouts.id=Workout_locations.workout_id"

但我得到这个错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“INNER JOIN Workout_locations ON Workouts.id=Workout_locations.workout_id”附近使用正确的语法

我希望对于有此经验的人来说,这是一个非常容易发现的错误。任何人都可以看到吗?谢谢!

4

1 回答 1

7

INNER JOIN应该在WHERE. 我也不认为您需要在您的子句周围BETWEEN加上括号,但我怀疑它是否会导致错误:

SELECT Workouts.date as date, Workout_locations.location_id as loc_id 
FROM Workouts 
INNER JOIN Workout_locations ON Workouts.id=Workout_locations.workout_id
WHERE Workouts.pacegroup_id = '9' 
AND Workouts.date BETWEEN '2013-08-19' AND '2013-08-25';

此外,虽然它们在技术上让您侥幸逃脱,但您应该避免使用“日期”作为选定的列名(这是一个保留字)。

您也可以进行一些简化以使内容更易于阅读:

SELECT Workouts.date AS wo_date, Workout_locations.location_id AS loc_id
FROM Workouts w
INNER JOIN Workout_locations l ON w.id = l.workout_id
WHERE w.pacegroup_id = '9'
AND w.date BETWEEN '2013-08-19' AND '2013-08-25';
于 2013-08-22T20:35:44.270 回答