在 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
关键字与其他函数结合,以便在执行查询时获取所需的结果。