如何删除sql子查询的使用 [英] How to remove use of sql sub queries

查看:400
本文介绍了如何删除sql子查询的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我们知道子查询需要花时间执行。删除子查询使用的最佳方法是什么?



我有2个表一个是Table1,另一个是Table2。像Table2这样的一对多映射具有单个Table1记录的多个记录。我希望为每一个表1行获得一行。



例如,如果表1中有一行 id: 1 和表2有3行,参考编号为1.



i不想获得3行,当我申请加入时会有结果是3行,我想仅从表2获得第一个记录,其中包含Table1行。我怎样才能实现这个目标?

Hi Friends,
As we know subquery take time in execution. What is the best way to remove use of subquery?

I have 2 table one is Table1 another is Table2. There is one to many mapping like Table2 has multiple record for a single Table1 record . i want to get a single row for each table 1 row.

For example if Table 1 has a row with id: 1 and Table 2 has 3 rows with reference id 1.

i don't want to get 3 rows, when i will apply join then there will be 3 rows as result, I want to get first record only from table 2 with a Table1 row. How can i achieve this?

推荐答案

如果没问题,请尝试以下方法。



选择*来自表1作为T1内连接(选择前1 *来自表2,其中Id = 1按顺序排序)为T1上的T2.Id = T2.Id
Try the below if it is ok.

Select * from Table1 as T1 inner join (Select Top 1 * From Table2 Where Id = 1 Order by Id Desc) as T2 on T1.Id = T2.Id


这篇关于如何删除sql子查询的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆