MySQL SELECT 产品 WHERE'每件产品的平均价格'<值?
mysqlmysqli database更新于 2025/11/3 1:48:17
首先我们创建一个表 −
mysql> create table DemoTable848( ProductId int, ProductPrice int ); Query OK, 0 rows affected (1.20 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable848 values(100,30); Query OK, 1 row affected (0.57 sec) mysql> insert into DemoTable848 values(101,50); Query OK, 1 row affected (1.06 sec) mysql> insert into DemoTable848 values(100,40); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable848 values(101,25); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable848 values(100,20); Query OK, 1 row affected (0.31 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable848;
这将产生以下输出 −
+-----------+--------------+ | ProductId | ProductPrice | +-----------+--------------+ | 100 | 30 | | 101 | 50 | | 100 | 40 | | 101 | 25 | | 100 | 20 | +-----------+--------------+ 5 rows in set (0.00 sec)
以下是选择产品的查询,其中"每件产品的平均价格" < 值。 这里,我们希望平均值小于 35。这只对 ProductId 为 100 的相应列值有效 −
mysql> select ProductId,avg(ProductPrice) from DemoTable848 group by ProductId having AVG(ProductPrice) < 35;
这将产生以下输出 −
+-----------+-------------------+ | ProductId | avg(ProductPrice) | +-----------+-------------------+ | 100 | 30.0000 | +-----------+-------------------+ 1 row in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

