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)

更新日期:2019 年 9 月 3 日

345 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始
廣告