在 SQL 中,可以使用 BETWEEN
關鍵字結合其他函數來執行更復雜的查詢。以下是一些示例:
BETWEEN
和 AVG()
函數獲取指定范圍內的平均值:SELECT AVG(price) as average_price
FROM products
WHERE price BETWEEN 10 AND 50;
BETWEEN
和 SUM()
函數獲取指定范圍內的總和:SELECT SUM(sales) as total_sales
FROM sales_data
WHERE date BETWEEN '2021-01-01' AND '2021-12-31';
BETWEEN
和 COUNT()
函數獲取指定范圍內的記錄數:SELECT COUNT(*) as total_records
FROM employees
WHERE salary BETWEEN 50000 AND 60000;
BETWEEN
和 MIN()
函數獲取指定范圍內的最小值:SELECT MIN(temperature) as lowest_temperature
FROM weather_data
WHERE date BETWEEN '2021-01-01' AND '2021-12-31';
BETWEEN
和 MAX()
函數獲取指定范圍內的最大值:SELECT MAX(temperature) as highest_temperature
FROM weather_data
WHERE date BETWEEN '2021-01-01' AND '2021-12-31';
這些示例展示了如何使用 BETWEEN
關鍵字與其他函數結合,以便在執行查詢時獲取所需的結果。