子查询中的MYSQL别名where

时间:2015-12-06 12:03:11

标签: mysql

我在子查询中遇到别名问题。

我有一个问题:

SELECT
    `id` as `t`, 
    `title`, 
    `once`,
    (select count(`id`) from `orders_files` where `order_id`='116815' and `type`=`t` and (`version`='41924') ) as `count`,
    (select count(`id`) from `orders_files` where `order_id`='116815' and `type`=`t` ) as `count_in_order`
FROM 
    `orders_filetypes`
WHERE
    `required`=1 
ORDER BY `sort`, `title`

运行并运行在旧版本的mysql(5.6.22)上,并且不适用于新版本的mysql(5.7.9)。 mysql错误是“#1054 - 'where子句'中的未知列't'。”

配置文件是相同的,问题是我不明白。

我们更新了mysql服务器,许多查询都停止了工作。 请告诉我,这个功能在较新的mysql中已被弃用,或者可以设置为/etc/my.cnf?

无法找到并修复数十万行代码,可以使用这些请求,因此最好在新服务器中包含这种可能性。

谢谢你!

1 个答案:

答案 0 :(得分:0)

为什么不使用表别名并按名称调用列:

SELECT
        `id` as `t`, 
        `title`, 
        `once`,
        (select count(`id`) from `orders_files` where `order_id`='116815' and `type`=outer_table.`id` and (`version`='41924') ) as `count`,
        (select count(`id`) from `orders_files` where `order_id`='116815' and `type`=outer_table.`id` ) as `count_in_order`
    FROM 
        `orders_filetypes` as outer_table
    WHERE
        `required`=1 
    ORDER BY `sort`, `title`