在我的sql查询的'where子句'中接收未知列'users personals.user_id'

问题描述 投票:2回答:1

我不知道为什么会这样,因为它们都存在:

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'
mysql sql database querying
1个回答
3
投票

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'
© www.soinside.com 2019 - 2024. All rights reserved.