English 中文(简体)
• 如何在MySQL进行复杂的问题
原标题:How to perform a complex range query in MySQL

我有一个表(表A),有1个编组(外勤)。 对于表A的每行,我想围绕B站点的分类价值进行一系列的+/100,然后从B站点找到这些幅度内的所有数值。 必须对B领域的所有价值进行询问。 每一行各行各行之间必须交回。 这方面的一个例子是我试图做的事情:

Table A
_______
A    1000    
B    3000    
C    5000    
D    1090   

采用以上表A,查询首先将发现B区所有分类账的幅度(+/-100)。

900 - 1100
2900 - 3100
4900 - 5100
990 - 1190

然后,询问会通过这些幅度,并从表A中排出收益,这些收益范围属于产生的范围。 采用上述例子,询问将返回:

A    1000
A    1000
B    3000
C    5000
D    1090
D    1090

<代码>A和D>因属于其各自范围而两次退还。 我如何构筑一个将每行各行各行之间回归的问道? 事先得到帮助。

最佳回答
SELECT t2.*
FROM tableA AS t1
INNER JOIN tableA AS t2 ON t2.fieldB >= (t1.fieldB - 100) AND t2.fieldB <= (t1.fieldB + 100)

是否还应显示“A”两度,因为它也出现在D系列中? (上述案件――如果不正确,请详细说明为什么^)

问题回答

首先是您的每个表A记录的最晚者。 随后又重新参加表A。 我补充说,符合资格的小组范围低,而是为了显示你正在寻找的限定性基础。 除D显示两度外,A还应显示“D”的距离为两倍。

select 
      a2.ShowLetter,
      a2.FieldB,
      GrpRanges.RangeLow,
      GrpRanges.RangeHi
   from 
      ( select distinct 
              a1.FieldB - 100 as RangeLow,
              a1.FieldB + 100 as RangeHi
           from
              TableA a1 ) GrpRanges

      JOIN TableA a2
         on a2.FieldB between GrpRanges.RangeLow and GrpRanges.RangeHi
   order by 
      a2.ShowLetter




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签