Mysql 使用update and join时出现未知列,即使该列存在

Mysql 使用update and join时出现未知列,即使该列存在,mysql,sql,Mysql,Sql,我使用下面的sql来更新一个名为userStats的表,它根据我提供的userId用户运行 update userStats us join (select sum(postStatus = 0) published, sum(postStatus = 1) inactive, sum(postStatus = 5) recalled, sum(postStatus = 6) deleted from userData where userId = @x) d o

我使用下面的sql来更新一个名为
userStats
的表,它根据我提供的
userId
用户运行

update userStats us
join (select
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted
from userData where userId = @x) d on d.userId = us.userId 
set
us.published = coalesce(d.published,0),
us.inactive = coalesce(d.inactive,0),
us.recalled = coalesce(d.recalled,0),
us.deleted = coalesce(d.deleted,0),
us.total = coalesce(d.published+d.inactive+d.recalled+d.deleted,0);
我一直在“on子句”中遇到错误
未知列“d.userId”
,即使该列存在于表中。我过去一直使用同样的东西,没有错误

另外,关于
where
的使用,在设置列之后是否需要另一个
where
,或者where内部是否足以只更新
用户id
I提供的信息。它只更新了我提供的
userId
,但是在where之后没有再使用一个
where
,我感到非常不舒服。我需要像以前那样在外面再做一次吗

us.deleted = coalesce(d.deleted,0),
us.total = coalesce(d.published+d.inactive+d.recalled+d.deleted,0) where userId =@x;

子查询中的更改

select
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted,userId 
from userData where userId = @x

然后在where子句中使用
userid
,它在子查询中起作用

select
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted,userId 
from userData where userId = @x

然后在where子句中使用
userid
,它在子查询中起作用

select
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted,userId 
from userData where userId = @x

然后在where子句中使用
userid
,它在子查询中起作用

select
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted,userId 
from userData where userId = @x

然后在where子句中使用
userid
,虽然
userData
表具有该值,但别名为
d
的表不会显式选择列
userid

因此,您可能应该做如下操作:

(select
    userID,  --> This field has to be selected to use in the join condition
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted
from userData where userId = @x) d on d.userId = us.userId 
                                         ^
                                         |___ You were getting error here.

别名为
d
的表没有显式选择列
userID
,尽管
userData
表具有该值

因此,您可能应该做如下操作:

(select
    userID,  --> This field has to be selected to use in the join condition
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted
from userData where userId = @x) d on d.userId = us.userId 
                                         ^
                                         |___ You were getting error here.

别名为
d
的表没有显式选择列
userID
,尽管
userData
表具有该值

因此,您可能应该做如下操作:

(select
    userID,  --> This field has to be selected to use in the join condition
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted
from userData where userId = @x) d on d.userId = us.userId 
                                         ^
                                         |___ You were getting error here.

别名为
d
的表没有显式选择列
userID
,尽管
userData
表具有该值

因此,您可能应该做如下操作:

(select
    userID,  --> This field has to be selected to use in the join condition
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted
from userData where userId = @x) d on d.userId = us.userId 
                                         ^
                                         |___ You were getting error here.

因为您只从内部查询中获得一行,所以不需要连接条件

update userStats us
join (select
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted
    from userData where userId = @x) d
set
us.published = coalesce(d.published,0),
us.inactive = coalesce(d.inactive,0),
us.recalled = coalesce(d.recalled,0),
us.deleted = coalesce(d.deleted,0),
us.total = coalesce(d.published+d.inactive+d.recalled+d.deleted,0)
where userid = @x
但在目标表中确实需要where子句


这个查询也会执行得很好,因为内部和外部查询都使用索引友好的wher子句。

因为您只从内部查询中获得一行,所以不需要连接条件

update userStats us
join (select
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted
    from userData where userId = @x) d
set
us.published = coalesce(d.published,0),
us.inactive = coalesce(d.inactive,0),
us.recalled = coalesce(d.recalled,0),
us.deleted = coalesce(d.deleted,0),
us.total = coalesce(d.published+d.inactive+d.recalled+d.deleted,0)
where userid = @x
但在目标表中确实需要where子句


这个查询也会执行得很好,因为内部和外部查询都使用索引友好的wher子句。

因为您只从内部查询中获得一行,所以不需要连接条件

update userStats us
join (select
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted
    from userData where userId = @x) d
set
us.published = coalesce(d.published,0),
us.inactive = coalesce(d.inactive,0),
us.recalled = coalesce(d.recalled,0),
us.deleted = coalesce(d.deleted,0),
us.total = coalesce(d.published+d.inactive+d.recalled+d.deleted,0)
where userid = @x
但在目标表中确实需要where子句


这个查询也会执行得很好,因为内部和外部查询都使用索引友好的wher子句。

因为您只从内部查询中获得一行,所以不需要连接条件

update userStats us
join (select
    sum(postStatus = 0) published,
    sum(postStatus = 1) inactive,
    sum(postStatus = 5) recalled,
    sum(postStatus = 6) deleted
    from userData where userId = @x) d
set
us.published = coalesce(d.published,0),
us.inactive = coalesce(d.inactive,0),
us.recalled = coalesce(d.recalled,0),
us.deleted = coalesce(d.deleted,0),
us.total = coalesce(d.published+d.inactive+d.recalled+d.deleted,0)
where userid = @x
但在目标表中确实需要where子句



这个查询也会执行得很好,因为内部和外部查询都使用索引友好的wher子句。

Hmm。。。同样的事情过去一直是这样。另外,在set列子句之后是否还需要一个额外的where。。。同样的事情过去一直是这样。另外,在set列子句之后是否还需要一个额外的where。。。同样的事情过去一直是这样。另外,在set列子句之后是否还需要一个额外的where。。。同样的事情过去一直是这样。另外,在set column子句之后是否还需要一个额外的where?我看到它可以使用。你能帮我处理一下
where
部分吗,在设置之后需要一个额外的
where
,或者里面的
where
可以吗?我认为不需要额外的where。你是基于条件加入的,你不需要再添加一个包含相同条件的where子句。我明白了,它是这样工作的。你能帮我处理一下
where
部分吗,在设置之后需要一个额外的
where
,或者里面的
where
可以吗?我认为不需要额外的where。你是基于条件加入的,你不需要再添加一个包含相同条件的where子句。我明白了,它是这样工作的。你能帮我处理一下
where
部分吗,在设置之后需要一个额外的
where
,或者里面的
where
可以吗?我认为不需要额外的where。你是基于条件加入的,你不需要再添加一个包含相同条件的where子句。我明白了,它是这样工作的。你能帮我处理一下
where
部分吗,在设置之后需要一个额外的
where
,或者里面的
where
可以吗?我认为不需要额外的where。您是基于该条件加入的,不需要再次添加包含相同条件的where子句。是的。我用了同样的东西,但不明白为什么它从今天起就不起作用了。即使没有用户ID,它也工作得很好。顺便说一句,你也可以在这里提供帮助,是的。我用了同样的东西,但不明白为什么它从今天起就不起作用了。即使没有用户ID,它也工作得很好。顺便说一句,你也可以在这里提供帮助,是的。我用了同样的东西,但不明白为什么它从今天起就不起作用了。即使没有用户ID,它也工作得很好。顺便说一句,你也可以在这里提供帮助,是的。我用了同样的东西,但不明白为什么它从今天起就不起作用了。即使没有用户ID,它也工作得很好。顺便说一句,你在这里也能帮上忙吗