Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL-获取多个最接近的值_Mysql_Sql - Fatal编程技术网

Mysql SQL-获取多个最接近的值

Mysql SQL-获取多个最接近的值,mysql,sql,Mysql,Sql,我想进行sql查询,搜索数据库以找到多个最接近的值 我有以下查询来查找最接近的值 SELECT * FROM table WHERE price >= (50 * .9) and price <= (50 * 1.1) order by abs(price - 50) LIMIT 1; 它工作正常,但我想让它搜索多个值,例如: SELECT * FROM table WHERE price >= (50 * .9) and price <= (50 * 1.1) //h

我想进行sql查询,搜索数据库以找到多个最接近的值 我有以下查询来查找最接近的值

SELECT * FROM table
WHERE price >= (50 * .9) and price <= (50 * 1.1)
order by abs(price - 50) LIMIT 1;
它工作正常,但我想让它搜索多个值,例如:

SELECT * FROM table
WHERE price >= (50 * .9) and price <= (50 * 1.1) //here i want one result (limit 1)
or price >= (50 * 1.9) and price <= (50 * 2.1) //here i want one result (limit 1)
order by abs(price - 50)
我想为每个价格限制1找不到所有的值。 我怎么能做到

//编辑 刚刚找到答案

(select *
  from table
  WHERE price >= (50 * .9) and price <= (50 * 1.1)
  order by abs(price - 50)
  limit 1
) union all
(select *
  from table
  WHERE price >= (50 * 1.9) and price <= (50 * 2.1)
  order by abs(price - 50)
  limit 1
)
使用union all怎么样

你想要这个吗

SELECT * FROM table
WHERE price >= (50 * .9) and price <= (50 * 1.1) //here i want one result    (limit 1)
 union
SELECT * FROM table
WHERE price >= (50 * 1.9) and price <= (50 * 2.1) //here i want one result  (limit 1)
 order by abs(price - 50)

ABSprice-2*50?是的,我刚在另一个网站上找到。无论如何,谢谢。因为您是按升序排序的,所以如果第一个查询不是空的,那么第二个查询将始终返回与第一个查询相同的结果。这使得第一个没用。
SELECT * FROM table
WHERE price >= (50 * .9) and price <= (50 * 1.1) //here i want one result    (limit 1)
 union
SELECT * FROM table
WHERE price >= (50 * 1.9) and price <= (50 * 2.1) //here i want one result  (limit 1)
 order by abs(price - 50)