您如何 select 3 个相邻行强调 MySQL 中较低的值?
How do you select 3 adjacent rows with emphasis on the lower values in MySQL?
我正在尝试弄清楚如何 select 从 table 的 3 个相邻行中获取价格 < 当前商品价格的方法。问题是,如果我 select 在 table 中的第一行、第二行或第三行,我需要 select 三个相邻的行 around 当前项目。需要强调价格较低的商品,例如,如果我 select 从 table 开始第三行,我需要 select 前两行和第四行排。到目前为止,这是我的查询:
SELECT *
FROM (SELECT *
FROM temp_db_cart
WHERE airport='$airport'
AND people='$people'
AND price < '$price'
ORDER BY price DESC LIMIT 3
)
ORDER BY price ASC
示例数据:
+---------------+---------+--------+-------+
| hotel_name | airport | people | price |
+---------------+---------+--------+-------+
| Days Inn | MLB | 1 | 109 |
| Holiday Inn | MCO | 2 | 149 |
| Americas Best | MLB | 2 | 199 |
| Econo Lodge | SFB | 1 | 209 |
+---------------+---------+--------+-------+
预期结果:
所选酒店:美洲最佳
+---------------+-------+---------+--------+-------+
| hotel_name | order | airport | people | price |
+---------------+-------+---------+--------+-------+
| Days Inn | 1 | .. | .. | .. |
| Holiday Inn | 2 | .. | .. | .. |
| Americas Best | Skip | .. | .. | .. |
| Econo Lodge | 3 | .. | .. | .. |
+---------------+-------+---------+--------+-------+
PHP/MySQL 组合可用于答案。任何帮助将不胜感激。
您可以union
价格较低的两条记录与价格较高的记录:
select * from
(
(SELECT *
FROM temp_db_cart
WHERE airport='MCO'
AND people='3'
AND price < '245'
ORDER BY price DESC LIMIT 3)
UNION
(SELECT *
FROM temp_db_cart
WHERE airport='MCO'
AND people='3'
AND price > '245'
ORDER BY price LIMIT 1)
)
order by price desc limit 3
在第一个查询中,您 select 3 行,在第二个查询中,1 行(如果存在)。最后,在这些 3+1
(或 3+0
)行中,您 select 只有 3
行的价格最高。
SELECT *
FROM (SELECT MIN(price)
FROM temp_db_cart
WHERE airport='$airport'
AND people='$people'
AND hotel != '$hotel'
ORDER BY price DESC LIMIT 3
)
ORDER BY price ASC
2个小时后,我终于找到了解决办法。我将联合与两个 select 查询组合在一起,即 selects 前 3 行(包括 selected 酒店)和 selected 酒店后 3 行。然后我设置 limit 3
和 order by price asc
和 一切 它主要工作。
SELECT *
FROM (
(
SELECT *
FROM temp_db_cart
WHERE price >= {$package['price']}
AND airport = '{$airport['abbr']}'
AND people = '{$travelers['number-of-travelers']}'
ORDER BY price ASC limit 3 )
UNION
(
SELECT *
FROM temp_db_cart
WHERE price < {$package['price']}
AND airport = '{$airport['abbr']}'
AND people = '{$travelers['number-of-travelers']}'
ORDER BY price DESC limit 3 ) ) AS u
WHERE hotel_name != '{$package['hotel_name']}'
ORDER BY price ASC limit 3;
编辑:父select查询中的3行限制只限于联合集中的前3行。我如何限制从 selected 酒店前 2 行开始到联合子集 selected 酒店后 2 行结束的偏移量?
我正在尝试弄清楚如何 select 从 table 的 3 个相邻行中获取价格 < 当前商品价格的方法。问题是,如果我 select 在 table 中的第一行、第二行或第三行,我需要 select 三个相邻的行 around 当前项目。需要强调价格较低的商品,例如,如果我 select 从 table 开始第三行,我需要 select 前两行和第四行排。到目前为止,这是我的查询:
SELECT *
FROM (SELECT *
FROM temp_db_cart
WHERE airport='$airport'
AND people='$people'
AND price < '$price'
ORDER BY price DESC LIMIT 3
)
ORDER BY price ASC
示例数据:
+---------------+---------+--------+-------+
| hotel_name | airport | people | price |
+---------------+---------+--------+-------+
| Days Inn | MLB | 1 | 109 |
| Holiday Inn | MCO | 2 | 149 |
| Americas Best | MLB | 2 | 199 |
| Econo Lodge | SFB | 1 | 209 |
+---------------+---------+--------+-------+
预期结果:
所选酒店:美洲最佳
+---------------+-------+---------+--------+-------+
| hotel_name | order | airport | people | price |
+---------------+-------+---------+--------+-------+
| Days Inn | 1 | .. | .. | .. |
| Holiday Inn | 2 | .. | .. | .. |
| Americas Best | Skip | .. | .. | .. |
| Econo Lodge | 3 | .. | .. | .. |
+---------------+-------+---------+--------+-------+
PHP/MySQL 组合可用于答案。任何帮助将不胜感激。
您可以union
价格较低的两条记录与价格较高的记录:
select * from
(
(SELECT *
FROM temp_db_cart
WHERE airport='MCO'
AND people='3'
AND price < '245'
ORDER BY price DESC LIMIT 3)
UNION
(SELECT *
FROM temp_db_cart
WHERE airport='MCO'
AND people='3'
AND price > '245'
ORDER BY price LIMIT 1)
)
order by price desc limit 3
在第一个查询中,您 select 3 行,在第二个查询中,1 行(如果存在)。最后,在这些 3+1
(或 3+0
)行中,您 select 只有 3
行的价格最高。
SELECT *
FROM (SELECT MIN(price)
FROM temp_db_cart
WHERE airport='$airport'
AND people='$people'
AND hotel != '$hotel'
ORDER BY price DESC LIMIT 3
)
ORDER BY price ASC
2个小时后,我终于找到了解决办法。我将联合与两个 select 查询组合在一起,即 selects 前 3 行(包括 selected 酒店)和 selected 酒店后 3 行。然后我设置 limit 3
和 order by price asc
和 一切 它主要工作。
SELECT *
FROM (
(
SELECT *
FROM temp_db_cart
WHERE price >= {$package['price']}
AND airport = '{$airport['abbr']}'
AND people = '{$travelers['number-of-travelers']}'
ORDER BY price ASC limit 3 )
UNION
(
SELECT *
FROM temp_db_cart
WHERE price < {$package['price']}
AND airport = '{$airport['abbr']}'
AND people = '{$travelers['number-of-travelers']}'
ORDER BY price DESC limit 3 ) ) AS u
WHERE hotel_name != '{$package['hotel_name']}'
ORDER BY price ASC limit 3;
编辑:父select查询中的3行限制只限于联合集中的前3行。我如何限制从 selected 酒店前 2 行开始到联合子集 selected 酒店后 2 行结束的偏移量?