重复列名'user_id'问题

时间:2011-04-25 22:53:41

标签: mysql count left-join

我在尝试根据用户的id计算用户记录数时遇到问题,但是我使用子查询来连接2个表,其中一个表有一个count参数,但是我收到错误{{1} }。

查询:

duplicate column name 'user_id

我需要的只是返回用户的信息和$sql = "SELECT loc.location_id, COUNT(loc.location_id) AS total_records FROM locations loc LEFT JOIN ( SELECT usr.*, loc.* FROM ( members usr INNER JOIN locations loc ) WHERE usr.user_id = " . $user_id . " AND usr.account_disabled = 0 ORDER BY loc.submit_date DESC ) usr ON (loc.user_id = usr.user_id)"; 函数完成的total_records计数。

干杯。

修改

这是我为此SQL返回的内容:

COUNT

enter image description here

2 个答案:

答案 0 :(得分:2)

为什么你得到派生的结果集或LEFT JOIN并不完全清楚。试试这个:

 SELECT    loc.location_id,
           COUNT(loc.location_id) AS total_records
 FROM        LOCATIONS_TABLE   loc
 INNER JOIN  MEMBERS_TABLE   usr
      ON (loc.user_id = usr.user_id)              
 WHERE loc.user_id =   $user_id  
 AND   usr.account_disabled = 0
 GROUP BY loc.location_id

答案 1 :(得分:1)

我认为问题在于这一部分:

SELECT usr.*,
       loc.*

两个表都有user_id