为什么以及如何编写SQL子查询 [英] Why and How to write SQL Sub queries

查看:103
本文介绍了为什么以及如何编写SQL子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以给我一个带有表模式的SQL Sub查询示例以及执行此操作的原因。



我在我以前的公司SELECT(Select * from)中看到了这个...类似的东西。我之前从未使用过子查询,我想知道他们服务的目的是什么。我不知道编写子查询的前一个程序员是在尝试做什么,但我知道你可以编写子查询。我不知道为什么。





任何例子都会受到赞赏。

解决方案

阅读本

子查询的力量 [ ^ ]


subQuries [ ^ ]


如果你想要一个列或一个值或一个where条件数据那么你可以使用sql子查询而不是编写复杂的连接等等



Ex 1.从xyz中选择名称,性别,(从abc中选择排名前1的学校名,其中xyzid = 1),其中id = 1



Ex 2.从xyz中选择姓名,性别,其中id =(从xyz中选择max(id))



Ex 3。从中选择abcxyz.Name(选择id,来自xyz的名称

其中id =(从xyz中选择max(id)))abcxyz;



你可以从 [^这里]

I was wondering if someone could give me an example of a SQL Sub query with a table schema and a reason for doing it.

I saw this at my previous company SELECT ( Select * from ) ...something like that. I have never used sub queries before and I'm wondering what purpose they serve .. I have no idea what the previous programmer who wrote the sub query was trying to do but I know that you can write sub queries. I just have no idea why .


Any example would be appreciated.

解决方案

Read this
The Power of Subqueries[^]


subQuries[^]


If You want a column or a value or a single where condition data then you can use sql sub query instead of writing complex join etc.

Ex 1. Select name,gender,(Select top 1 schoolname from abc where xyzid = 1) from xyz where id=1

Ex 2. Select name,gender from xyz where id=(select max(id) from xyz)

Ex 3. Select abcxyz.Name from (select id,Name from xyz
where id=(select max(id) from xyz) ) abcxyz;

You can learn from [^here]


这篇关于为什么以及如何编写SQL子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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