面对这样的问题 - 我无法将这两个请求结合起来,怎么样?来自一个表的所有数据
SELECT
max(f.price) AS max_rent,
min(f.price) AS min_rent
FROM Commercial f
WHERE f.type = 1;
SELECT
max(f.price) AS max_sale,
min(f.price) AS min_sale
FROM Commercial f
WHERE f.type = 0;
收到结果。 2个结果表
+---------+---------+ +---------+---------+
| max_sale| min_sale| |max_sale | min_sale|
+---------+---------+ +---------+---------+
| 23 | 45 | | 15 | 52 |
+---------+---------+ +---------+---------+
预期结果。 1表结果
+---------+---------+---------+---------+
| max_sale| min_sale| max_sale| min_sale|
+---------+---------+---------+---------+
| 23 | 45 | 15 | 52 |
+---------+---------+---------+---------+
答案 0 :(得分:1)
如何使用group by
?
SELECT f.type,
max(f.price) AS max_price_for_sale,
min(f.price) AS min_price_for_sale
FROM Commercial f
WHERE f.type IN (0, 1)
GROUP BY f.type;
答案 1 :(得分:0)
只需在中间添加联合
SELECT
max(f.price) AS max_price_for_rent,
min(f.price) AS min_price_for_rent
FROM Commercial f
WHERE f.type = 1;
UNION
SELECT
max(f.price) AS max_price_for_sale,
min(f.price) AS min_price_for_sale
FROM Commercial f
WHERE f.type = 0;
如果您想保留重复项,请添加UNION ALL
但是,对于此特定查询,使用GROUP BY
和Gordon Linoff的答案会更好。
答案 2 :(得分:0)
这对你有用吗?它应该沿着一行返回4列:
SELECT
C1.max_rent,
C1.min_rent,
C0.max_rent,
C0.min_rent
FROM
(
SELECT
max(f.price) AS max_rent,
min(f.price) AS min_rent
FROM Commercial f
WHERE f.type = 1) C1,
(
SELECT
max(f.price) AS max_rent,
min(f.price) AS min_rent
FROM Commercial f
WHERE f.type = 0) C0