【问题标题】:MySql every derived table must have its own alias. Even when all haveMySql 每个派生表都必须有自己的别名。即使所有人都有
【发布时间】:2013-11-18 14:12:21
【问题描述】:
UPDATE  likesd a
    INNER JOIN (
select id, perCent, rank
from
(
    select id, perCent, elmTotals 
    , @curRank :=   
        case 
           when @prevParent = parent then
               case 
                   when @prevCent = perCent then @curRank
                   else @curRank + 1
               end
           else 1
        end rank
    , @prevParent := parent
    , @prevCent := perCent
    from 
    (
        select child.id, child.perCent, child.parent
        from likesd parent
        inner join likesd child
           on child.parent = parent.id and child.country = parent.country
        where parent.type = 3
        order by parent.id, child.perCent desc
    ) b
    cross join (SELECT @curRank := 0, @prevParent := null, @prevCent := null) c
) d ) ON a.country = d.country and a.id = d.id
    SET a.rank = d.rank
    WHERE a.type = 10;

在上面的 sql 中,我得到错误:/* SQL Error (1248): Every derived table must have its own alias */ 即使我的所有派生表都有别名。如果没有围绕主 sql 进行更新,一切正常。但是当我添加更新时,我得到了这个错误。

最终结果是使用来自选择的rank 更新列rank。只有更新才会出现问题。里面有大选择就好了。

这是下表,这是带有一些数据的fiddle

主表

"id"    "type"  "parent"    "country"   "votes" "perCent" "rank"
"24"    "1"     "1"         "US"        "35"    "0"
"25"    "3"     "24"        "US"        "35"    "0"
"26"    "10"    "25"        "US"        "15"    "50.00"
"27"    "10"    "25"        "US"        "10"    "33.33"
"28"    "10"    "25"        "US"        "10"    "33.33"

"29"    "1"     "1"         "US"        "50"    "0"
"30"    "3"     "29"        "US"        "50"    "0"
"31"    "10"    "30"        "US"        "20"    "40.00"
"32"    "10"    "30"        "US"        "15"    "25.00"
"33"    "10"    "30"        "US"        "15"    "35.00"

【问题讨论】:

  • sqlfiddle 是否适合其他人?
  • @Mihai 是的,它已经关闭了。

标签: mysql sql


【解决方案1】:
UPDATE  likesd a
    INNER JOIN (
select id, perCent, rank
from
(
    select id, perCent, elmTotals 
    , @curRank :=   
        case 
           when @prevParent = parent then
               case 
                   when @prevCent = perCent then @curRank
                   else @curRank + 1
               end
           else 1
        end rank
    , @prevParent := parent
    , @prevCent := perCent
    from 
    (
        select child.id, child.perCent, child.parent
        from likesd parent
        inner join likesd child
           on child.parent = parent.id and child.country = parent.country
        where parent.type = 3
        order by parent.id, child.perCent desc
    ) b
    cross join (SELECT @curRank := 0, @prevParent := null, @prevCent := null) c
) d )f ON a.country = d.country and a.id = d.id
    SET a.rank = d.rank
    WHERE a.type = 10;

【讨论】:

  • 我在 cmets 上将 )f 发布为 ) missingTableAlias。你先得到它:)
  • 在on子句中,现在应该是a。与 f。或一个。与 d.?我不断收到Unknown column 'd.country' in 'on clause
  • 我觉得应该是a.country = f.country and a.id = f.id
  • 一件小事,我是否也应该在WHERE 子句中指定连接条件以使事情更安全?
  • 我不认为把条件也放在哪里会使事情更安全。
【解决方案2】:

您缺少一个别名:

cross join (SELECT @curRank := 0, @prevParent := null, @prevCent := null) c
) d ) ON a.country = d.country and a.id = d.id
     ^ Here

【讨论】:

    猜你喜欢
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2014-04-30
    相关资源
    最近更新 更多