我在尝试根据用户的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
答案 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