MalFormed MySQL查询

时间:2013-12-19 16:31:01

标签: mysql sql

这是查询。

SELECT pl.id, pl.latitude, pl.longitude, pl.userId, pl.time, pl.type, com.id as com_id, com.text, com.time as com_time,
 (SELECT name as com_name, email as com_email, picture as com_picture
  FROM  aa_users 
  LEFT JOIN aa_comments ON aa_users.id = aa_comments.user_id)
usr.name as com_usr_name,
usr.email as com_usr_email,
usr.picture as com_usr_picture,
usr.type as com_usr_type FROM aa_places as pl  
LEFT JOIN aa_comments as com ON ( pl.id = com.place_id )   
LEFT JOIN aa_users as usr ON (pl.userId = usr.id)   

第二个选择失败。 我需要做的是获得两种类型的用户:一个地方的用户所有者(user.name,user.email等等)和评论的用户所有者

 (SELECT name as com_name, email as com_email, picture as com_picture
  FROM  aa_users 
  LEFT JOIN aa_comments ON aa_users.id = aa_comments.user_id)

我不能让它发挥作用。我该怎么改变???

1 个答案:

答案 0 :(得分:1)

您的内部选择位于主查询的选择中。这是合法的,但是这样做只能返回一列(和一行) - 您的查询返回三列,并且可能有很多行,因为它没有条件。

我不清楚你要做什么,但我认为这会让你更接近:

SELECT 
    pl.id, pl.latitude, pl.longitude, pl.userId, pl.time, pl.type, com.id as com_id, com.text, com.time as com_time,
    com_usr.name as com_name, com_usr.email as com_email, com_usr.picture as com_picture,
    usr.name as com_usr_name,
    usr.email as com_usr_email,
    usr.picture as com_usr_picture,
    usr.type as com_usr_type 
FROM aa_places as pl  
LEFT JOIN aa_comments as com ON ( pl.id = com.place_id )   
LEFT JOIN aa_users as com_usr ON  (com.user_id=com_usr.id)
LEFT JOIN aa_users as usr ON (pl.userId = usr.id)

根据评论更新。