简体   繁体   English

如何加入两个查询以进行子查询

[英]How to join two queries to make a subquery

I have a first query that give me a set of values and a second query that give me the maximum of this set of value.我有一个给我一组值的第一个查询和一个给我这组值的最大值的第二个查询。 I want to merge these two queries.我想合并这两个查询。 I tried like this below:我在下面尝试过这样的:

First query:第一个查询:

SELECT SUBSTR(column, INSTR(column, ' ')+1,3)  
from table  
WHERE column LIKE '#13 %'

Second query:第二个查询:

SELECT MAX(column)

The merge:合并:

SELECT MAX(column) 
FROM table WHERE column = (
  SELECT  SUBSTR(column, INSTR(column, ' ')+1,3)  
  from table  
  WHERE column LIKE '#13 %'
)

Can you please help how can I merge two queries?你能帮我如何合并两个查询吗?

First query result:第一次查询结果:

30
1
2
3
12
13
14
15
16
17
18
19

I want to have the maximum value of this set of values with my second query: 30 .我想在我的第二个查询中获得这组值的最大值: 30

The result of SUBSTR(column, INSTR(column, ' ') + 1, 3) is a string, so you must cast it to a number and this can be done simply by adding 0 . SUBSTR(column, INSTR(column, ' ') + 1, 3)的结果是一个字符串,因此您必须将它转换为一个数字,这可以简单地通过添加0来完成。
Then use MAX() aggregate function:然后使用MAX()聚合 function:

SELECT MAX(SUBSTR(column, INSTR(column, ' ') + 1, 3) + 0)  
FROM tablename  
WHERE column LIKE '#13 %';

Since you have the condition column LIKE '#13 %' in the WHERE clause, then you know that the 1st space in the column is at position 4, so you could simplify to:由于WHERE子句中有条件column LIKE '#13 %' ,因此您知道列中的第一个空格位于 position 4,因此您可以简化为:

SELECT MAX(SUBSTR(column, 5, 3) + 0)  
FROM tablename  
WHERE column LIKE '#13 %';

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

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