简体   繁体   English

SQL 将一个查询的结果用于另一个查询

[英]SQL Use Result from one Query for another Query

This is an excerpt from one table:这是一张表的摘录:

| id | type    | other_id | def_id | ref_def_id|
| 1  | int     | NULL     |  5     | NULL     |
| 2  | string  | NULL     |  5     | NULL     |
| 3  | int     | NULL     |  5     | NULL     |
| 20 | ref     | 3        |  NULL  | 5        |
| 21 | ref     | 4        |  NULL  | 5        | 
| 22 | ref     | 5        |  NULL  | 5        |  

What I want is to find entries with type ref.我想要的是找到类型为 ref 的条目。 Then I would for example have this one entry in my result:然后我会在我的结果中包含这个条目:

| 22 | ref     | 5        |  NULL  | 5        |  

The problem I am facing is that I now want to combine this entry with other entries of the same table where def_id = 5.我面临的问题是,我现在想将此条目与 def_id = 5 的同一表的其他条目结合起来。

So I would get all entries with def_id = 5 for this specific ref type as result.所以我会得到这个特定引用类型的 def_id = 5 的所有条目作为结果。 I somehow need the output from my first query, check what the ref_def_id is and then make another query for this id.我不知何故需要我的第一个查询的输出,检查 ref_def_id 是什么,然后对该 id 进行另一个查询。

I really have problems to understand how to proceed.我真的很难理解如何继续。 Any input is much appreciated.任何输入都非常感谢。

If I understand correctly you need to find rows with a type of 'ref' and then use the values in their ref_def_id columns to get the rows with the same values in def_id .如果我理解正确的话,你需要找到一个行type “裁判”,然后使用自己的价值观ref_def_id列在相同的值来获得行def_id In that case you need to use a subquery for getting the rows with 'ref' type and combine it using either IN or EXISTS :在这种情况下,您需要使用子查询来获取具有 'ref' 类型的行,并使用INEXISTS将其组合:

select *
from YourTable
where def_id in (select ref_def_id from YourTable where type='ref');

select *
from YourTable
where exists (select * from YourTable yt
  where yt.ref_def_id=YourTable.def_id and yt.type='ref')

Both queries are equivalent, IN is easier to understand at first sight but EXISTS allow more complex conditions (for example you can use more than one column for combining with the subquery).这两个查询是等价的, IN乍一看更容易理解,但EXISTS允许更复杂的条件(例如,您可以使用多个列与子查询组合)。

Edit: since you comment that you need also the id from the 'ref' rows then you need to use a subquery:编辑:因为您评论说您还需要来自'ref'行的id ,那么您需要使用子查询:

select source_id, YourTable.*
from YourTable
join (select id as source_id, ref_def_id
      from YourTable
      where type='ref')
as refs on refs.ref_def_id=YourTable.def_id
order by source_id, id;

With this for each 'ref' row you would get all the rows with the associated ref_id .对于每个 'ref' 行,您将获得具有关联ref_id所有行。

What you are looking for is a subquery or even better a join operation.您正在寻找的是子查询,甚至更好的连接操作。

Have a look here: http://www.mysqltutorial.org/mysql-left-join.aspx看看这里: http : //www.mysqltutorial.org/mysql-left-join.aspx

Joins / the left join allows you to combine rows of tables within one query on a given condition.联接/左联接允许您根据给定条件在一个查询中组合表行。 The condition could be id = 5 for your purpose.出于您的目的,条件可能是 id = 5。

You would seem to want aggregation:你似乎想要聚合:

select max(id) as id, type, max(other_id) as other_id,
       max(def_id) as def_id, ref_def_id
from t
where type = 'ref'
group by type, ref_def_id

use below query to get column from sub query.使用下面的查询从子查询中获取列。

select a.ref_def_id
from (select ref_def_id from YourTable where type='ref') as a;

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

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