简体   繁体   English

具有相同条件的多个 SELECTS

[英]Multiple SELECTS with same condition

I have a table with approximately 700 data with auto-incremental ID and with these fields:我有一个包含大约 700 个数据的表,带有自动增量 ID 和这些字段:

+------+------+------+------+
| id | pos_1 | pos_2 | pos_3 |
+------+------+------+------+
| 1  |   9   |   2   |   3   |
| 2  |   3   |   2   |   9   |
| 3  |   1   |   4   |   3   |
| 4  |   1   |   2   |   3   |
| 5  |   5   |   1   |   6   |
| 6  |   1   |   2   |   3   |
| 7  |   8   |   2   |   0   |
| ...| ...   |  ...  |   ... |
| etc|   etc |   etc |   etc |
+------+------+------+------+

It happens that I always perform a query that contains several selects and a common condition, all united by "UNION" in this way:碰巧我总是执行一个包含多个选择和一个共同条件的查询,所有这些都由“UNION”以这种方式联合起来:

select *
from listado T1
 where pos_1 = ***  and pos_2 = ***  and pos_3 = ***       and exists (
   select id
   from listado T2
   where
 T2.pos_1 = ***  and pos_2 = ***  and pos_3 = ***
and T2.id >= T1.id - 10
and T2.id <= T1.id + 10
and T1.id != T2.id )

 UNION

 select *
from listado T3
 where pos_1 = ***  and pos_2 = ***  and pos_3 = ***    and exists (
   select id
   from listado T4
   where
 T4.pos_1 = ***  and pos_2 = ***  and pos_3 = ***
and T4.id >= T3.id - 10
and T4.id <= T3.id + 10
and T3.id != T4.id )

 UNION

 select *
from listado T5
 where pos_1 = ***  and pos_2 = ***  and pos_3 = ***    and exists (
   select id
   from listado T6
   where
 T6.pos_1 = ***  and pos_2 = ***  and pos_3 = ***
and T6.id >= T5.id - 10
and T6.id <= T5.id + 10
and T5.id != T6.id )

 ORDER BY id ASC

In such a way that this query returns the rows that match the numbers that I have entered in the corresponding positions (pos_1, pos_2, pos_3) of the table, BUT with the condition that the RESULTS of this search do not DISTANCE from each other more of 10 POSITIONS per ID as you can see what I do with the fragment:通过这种方式,此查询返回与我在表的相应位置(pos_1、pos_2、pos_3)中输入的数字相匹配的行,但条件是此搜索的结果彼此之间不再有距离每个 ID 有 10 个位置,你可以看到我对片段做了什么:

and T***.id >= T***.id - 10
        and T***.id <= T***.id + 10
 and T***.id != T***.id )

Up to here everything is correct, the problem is that when making this query as it is done the RESULTS that I returned are the SELECTS independently from each other, that is, it only shows me the data I consult individually GROUPED SELECTS of the others ... I mean you are omitting ALL the results that intermingle the rows of the 3 SELECTS if that is the way it should be because it appears in the table.到这里一切都是正确的,问题是在完成这个查询时,我返回的 RESULTS 是彼此独立的 SELECTS,也就是说,它只向我显示我单独查阅的数据 GROUPED SELECTS of others 。 .. 我的意思是你省略了所有混合 3 个 SELECTS 行的结果,如果这是应该的方式,因为它出现在表中。 I'm getting the results SEPARATELY and I need the SELECTS to be united as a single SELECT and do not separate me and divide the results into three different SELECTS ... and I have no idea how to accommodate the query to do so ... Sharing the condition that the results do not differ from each other by ID more than 10 positions and jointly FOR EXAMPLE the result line of the SELECT nº1 is within the line of the result of SELECT Nº2 or Nº3 if it should be because it is so In the table.我单独得到结果,我需要将 SELECTS 合并为一个 SELECT 并且不要将我分开并将结果分成三个不同的 SELECTS ...我不知道如何适应查询来这样做.. . 共享结果不因 ID 不同而超过 10 个位置的条件,并且共同例如,SELECT nº1 的结果行在 SELECT Nº2 或 Nº3 的结果行内,如果应该是因为它是这样在表中。

thank you for your attention.感谢您的关注。 The example at the end is simply that the result of each of these 3 selects is not separated and independent among them, but that the results of the 3 are intermingled as if it were a single broad selection.最后的例子很简单,这 3 个选择中的每一个的结果都不是在它们之间分离和独立的,而是将 3 个的结果混合在一起,就好像它是一个单一的广泛选择一样。 And with the same condition that the 3 share that the results do not have more than 10 id positions between them.并且在相同的条件下,3 个共享结果之间的 id 位置不超过 10 个。

If I ask you to do the search for the rows: 1st SELECT: 0 - 1 - 4 2nd SELECT: 2 - 4 - 8 3rd SELECT: 3 - 5 - 6如果我让你搜索行: 1st SELECT: 0 - 1 - 4 2nd SELECT: 2 - 4 - 8 3rd SELECT: 3 - 5 - 6

What you will do right now, as the query is done, will show me this:查询完成后,您现在将执行的操作将显示给我:

82  0   1   4
83  0   1   4
86  0   1   4

On the other hand:另一方面:

16  2   4   8
20  2   4   8
24  2   4   8

And on the other:另一方面:

256 3   5   6
260 3   5   6
262 3   5   6

If you look at the results, apparently they are correct because they are showing me what I have asked for and also the results are grouped by an id that is not more than 10 positions apart.如果您查看结果,显然它们是正确的,因为它们向我展示了我的要求,并且结果按不超过 10 个位置的 id 分组。 But, this omitting all the results that are mixed and so appear in the table as for example would be:但是,这省略了所有混合的结果,因此出现在表中,例如:


82  0   1   4
84  3   5   6
86  0   1   4
89  3   5   6
--- --- --- ---
90  3   5   6
91  2   4   8

This last result would be correct since it would be located in the table, the lines are as they are supposed to appear in the list of data and only shows me that group of rows BECAUSE IT IS THE ONLY ONE OF ALL THE POSSIBLE THAT IS NEARLY NEXT GROUPED BY ID = 10.最后一个结果是正确的,因为它位于表中,这些行应该出现在数据列表中,并且只向我显示那组行,因为它是所有可能的行中唯一的一个下一个按 ID 分组 = 10。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM