我不知道为什么会这样,因为它们都存在:
SELECT (
SELECT COUNT(user_id)
FROM users u
WHERE
u.id=users_personals.user_id
AND u.status NOT IN (1,2,7)
LIMIT 1
) as Total
FROM users_personals p
WHERE
p.neighborhoods='Miami Beach'
AND DATE_FORMAT(created_at, '%Y-%m')>'2018-03'
表users_personals
在查询中别名为p
。
你可能想改变这个:
WHERE u.id=users_personals.user_id
至:
WHERE u.id=p.user_id
注意:就问题而言,您的SQL可能会被简化以避免需要内联查询。
没有看到样本数据和预期输出就无法确定......这是一个疯狂的猜测:
SELECT COUNT(*) as Total
FROM users_personals p
INNER JOIN users u
ON u.id = p.user_id
AND u.status NOT IN (1,2,7)
WHERE
p.neighborhoods = 'Miami Beach'
p.created_at >= '2018-04-01'