mysql在where子句中明确引用table.column

时间:2014-03-07 02:29:33

标签: mysql

有没有理由说MySQL不喜欢我的where子句中的以下显式引用。

where [table]。[column]?

我收到错误无效的查询:'where子句'中的未知列'plugin_thold_log.id'

这适用于TSQL。

'plugin_thold_log是我数据库中的一个表,id显然是同一个表中的一列。

我的完整查询:

这是我的完整查询:

select pl.id as id, 
from_unixtime(pl.time) as time,
case
when TIMESTAMPDIFF(MINUTE,(select from_unixtime(a.time) from plugin_thold_log a where a.id < plugin_thold_log.id and a.time >= '$dateStart' and a.time <= '$dateEnd' order by a.id desc LIMIT 1),from_unixtime(pl.time)) is null then 1
when TIMESTAMPDIFF(MINUTE,(select from_unixtime(a.time) from plugin_thold_log a where a.id < plugin_thold_log.id and a.time >= '$dateStart' and a.time <= '$dateEnd' order by a.id desc LIMIT 1),from_unixtime(pl.time)) > 30 then 1
when TIMESTAMPDIFF(MINUTE,(select from_unixtime(a.time) from plugin_thold_log a where a.id < plugin_thold_log.id and a.time >= '$dateStart' and a.time <= '$dateEnd' order by a.id desc LIMIT 1),from_unixtime(pl.time)) < 30
    and TIMESTAMPDIFF(MINUTE,(select from_unixtime(a.time) from (select from_unixtime(a.time) from plugin_thold_log a where a.id < plugin_thold_log.id and a.time >= '$dateStart' and a.time <= '$datEend' order by a.id desc LIMIT 7) a order by 1 asc LIMIT 1),from_unixtime(pl.time)) < 30 then 0
when TIMESTAMPDIFF(MINUTE,(select from_unixtime(a.time) from plugin_thold_log a where a.id < plugin_thold_log.id and a.time >= '$dateStart' and a.time <= '$dateEnd' order by a.id desc LIMIT 1),from_unixtime(pl.time)) < 30
    and TIMESTAMPDIFF(MINUTE,(select from_unixtime(a.time) from (select from_unixtime(a.time) from plugin_thold_log a where a.id < plugin_thold_log.id and a.time >= '$dateStart' and a.time <= '$datEend' order by a.id desc LIMIT 7) a order by 1 asc LIMIT 1),from_unixtime(pl.time) ) is null then 0
else 1
end
from
plugin_thold_log pl

where
pl.time >= '$dateStart'
and pl.time <= '$dateEnd'
order by 1

1 个答案:

答案 0 :(得分:0)

我无法重现问题,测试代码按预期工作:

/*Table structure for table `plugin_thold_log` */

DROP TABLE IF EXISTS `plugin_thold_log`;

CREATE TABLE `plugin_thold_log` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=INNODB;

/*Data for the table `plugin_thold_log` */

INSERT  INTO `plugin_thold_log`(`id`) VALUES (NULL);

SELECT `id`
FROM `plugin_thold_log`
WHERE `plugin_thold_log`.`id` = 1;

SQL Fiddle demo

<强>更新

/*
Error Code : 1054
Unknown column 'plugin_thold_log.id' in 'where clause'

SELECT
    (   SELECT `id`
        FROM `plugin_thold_log` `a`
        WHERE `a`.`id` != `plugin_thold_log`.`id`
    )
FROM `plugin_thold_log` `pl`;
*/

变化:

SELECT
    (   SELECT `id`
        FROM `plugin_thold_log` `a`
        WHERE `a`.`id` != `pl`.`id`
    )
FROM `plugin_thold_log` `pl`;