mysql查询语法错误,MySQL内部联接查询语法错误

I'm kind of a MySQL novice and can't figure out what is going wrong here. I have two tables. The left table is called Workouts. The relevant columns are date (type date) and id (type int).

The right table is called Workout_locations (relevant fields: workout_id type int, and location_id type int).

The join fields are Workouts.id and Workout_locations.workout_id.

All I want to do is get a table of two columns: date (from Workouts), and location_id (from Workout_locations). I need to only pull records from the Workouts table based on a couple of fields (the sql statement should make this clear).

Here is my sql syntax:

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"

But I get this error:

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 'INNER JOIN Workout_locations ON

Workouts.id=Workout_locations.workout_id' at line 1

I'm hoping that this is a very easy error to spot for someone who is experienced with this. Can anyone see it? Thanks!

解决方案

Your INNER JOIN should come before the WHERE. I also don't think you needed the parens around your BETWEEN clause, but I doubt that it'll cause an error either way:

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';

Also, though they technically let you get away with it, you should avoid using "date" as a selected column name (it's a reserved word).

You could do a bit of streamlining as well to make things a bit easier to read:

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';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值