MySQL WHERE "每个产品的平均价格" < value 中 `SELECT` products?
让我们先创建一个表 -
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)
以下是 WHERE "每个产品的平均价格" < value 中选择 products 的查询。这里,我们希望平均值小于 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)
广告