[英]Getting confused by MySQL subqueries
I'm trying to learn how to do subqueries, and I'm really confused at what's wrong with this simple example.我正在尝试学习如何进行子查询,我真的很困惑这个简单的例子有什么问题。
My first try was我的第一次尝试是
SELECT COUNT(SELECT * FROM my_table);
but that didn't work (I guess because I need a temporary table?) so I tried this:但这不起作用(我猜是因为我需要一个临时表?)所以我尝试了这个:
SELECT COUNT(items)
FROM (SELECT * FROM my_table) AS items;
Why do I get the following:为什么我会得到以下信息:
1054: Unknown column 'items' in 'field list' 1054:“字段列表”中的未知列“项目”
You're getting the error because in this example items
is a table (as it is an alias), not a column.您收到错误是因为在此示例items
是表(因为它是别名),而不是列。 Simplest solution is to use:最简单的解决方案是使用:
SELECT COUNT(*)
FROM (SELECT * FROM my_table) AS items
Aggregate functions (IE: COUNT, MIN, MAX, AVG, etc) only work on column references, but some accept [table].* as a parameter.聚合函数(即:COUNT、MIN、MAX、AVG 等)仅适用于列引用,但有些接受 [table].* 作为参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.