English 中文(简体)
MySQL: [rr] 1146 - 表格别名不存在
原标题:MySQL: [Err] 1146 - table alias doesn t exist
  • 时间:2012-05-25 10:34:42
  •  标签:
  • mysql

我在找一样的东西

SELECT
    `foo`.*,
    (SELECT MAX(`foo`.`bar`) FROM `foo`)
FROM
    (SELECT * FROM `fuz`) AS `foo`;

但似乎 foo 在嵌套查询中不被识别, 因为错误类似

[Err] 1146 - Table  foo  doesn t exist

我尝试上面的查询 因为我认为它比类似的东西更快

SELECT
    `fuz`.*,
    (SELECT MAX(`bar`) FROM `fuz`) as max_bar_from_fuz
FROM `fuz`

请给我一些建议

EDIT: 我正寻找比第二个查询性能更好的解决方案。 请假设我的表格 < code> fuz 是一个非常大的一个, 从而运行一个额外的查询, 获取 < code> max_bar 需要我花费很多钱 。

问题回答

对于首个查询(经过一些修改),您想要的工作被称为“共同表格表达式”,而 MySQL 不具备这一特性。

如果您的第二个查询工作不顺利, 您可以使用此选项 :

SELECT
    fuz.*,
    fuz_grp.max_bar
FROM 
    fuz
  CROSS JOIN
    ( SELECT MAX(bar) AS max_bar
      FROM fuz
    ) AS fuz_grp

创建为 SELECT 条款的 Alias 只能用于访问 scalar 值, 它们是表格的 < 坚固 > 不 < / 坚固 > 等同名词。 如果您想要返回所有返回行的列的最大值, 您可以先运行一个查询, 将最大值计算成变量, 然后将此变量用作您的查询的 scalar 值, 例如 :

-- create and populate a table to demonstrate concept
CREATE TABLE fuz (bar INT, col0 VARCHAR(20), col1 VARCHAR(20) );
INSERT INTO fuz(bar, col0, col1) VALUES (1,  A ,  Airplane );
INSERT INTO fuz(bar, col0, col1) VALUES (2,  B ,  Boat );
INSERT INTO fuz(bar, col0, col1) VALUES (3,  C ,  Car );

-- create the scalar variable with the value of MAX(bar)
SELECT @max_foo:=MAX(bar) FROM fuz;

-- use the scalar variable into the query
SELECT *, @max_foo AS `MAX_FOO`
FROM fuz;

-- result:
-- | BAR | COL0 |     COL1 | MAX_FOO |
-- |-----|------|----------|---------|
-- |   1 |    A | Airplane |       3 |
-- |   2 |    B |     Boat |       3 |
-- |   3 |    C |      Car |       3 |

简单使用 MAX 函数 :

SELECT
    `fuz`.*,
    MAX(`fuz`.`bar`)
FROM
    `fuz`

或使用

SELECT
    `foo`.*,
    MAX(`foo`.`bar`)
FROM
    (SELECT * FROM `fuz` JOIN `loolse  ON (`fuz`.`field` = `loolse`.`smile`)) AS `foo`;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签