0

What I'd like to do is for a gaming application I need to search for my non player characters and they have a field called target_id that stores the id of whatever they are chasing.

given:
table mobs with fields and some example data
id   x   y   target_id
1    1   1   2
2    3   3   1

I would like to return the following data with a query

id   x   y   target_id target_x target_y
1    1   1   2         3        3
2    3   3   1         1        1

This is what I tried but it has syntax errors

SELECT id,x,y,target_id, target.x, target.y FROM mobs LEFT JOIN mobs AS target ON      target_id=id FROM mobs WHERE 1
4

2 回答 2

2

据我所知,正确的语法是:

SELECT mobs.id, mobs.x, mobs.y, mobs.target_id, target.x, target.y 
FROM mobs 
LEFT JOIN mobs AS target 
ON      mobs.target_id = target.id

您已经通过说“JOIN”指定了“FROM”,当您不需要过滤任何行时,您不需要 WHERE

编辑:对不起,我没有把别名放进去——你需要确保最后一部分(和选择中的字段)有你想要定位的表——记住,如果你有两个表,说 target_id 没有多大意义在具有相同列名的查询中。否则你会得到一个模棱两可的错误。我已将别名添加到查询中

于 2012-07-31T15:47:56.850 回答
2

我不知道你为什么有 aWHERE 1或 second FROM,但我相信这就是你想要的:

SELECT M.id, M.x, M.y, M.target_id, T.x, T.y 
FROM mobs AS M
LEFT JOIN mobs AS T 
ON M.target_id = T.id 

将来,您应该发布您的表定义和预期结果,以便您的问题更加清晰。

于 2012-07-31T15:49:17.507 回答