Mysql SELECT、FROM或WHERE中嵌套的SELECT子句和差异

Mysql SELECT、FROM或WHERE中嵌套的SELECT子句和差异,mysql,sql,tsql,Mysql,Sql,Tsql,1.我想知道我们什么时候在select子句中使用子查询的结构 2.如果在1中写入任何查询。结构具有其他嵌套形式之一的等效项: SELECT ... from ... where .. IN (SELECT ...) SELECT ... from (SELECT ...) 谢谢为了回答这个问题,我将第一个选择称为外部选择,第二个选择称为内部选择 Get the population of cities which are in Texas SELECT city, populati

1.我想知道我们什么时候在select子句中使用子查询的结构

2.如果在1中写入任何查询。结构具有其他嵌套形式之一的等效项:

  SELECT ... from ... where .. IN (SELECT ...)
    SELECT ... from (SELECT ...)

谢谢

为了回答这个问题,我将第一个选择称为外部选择,第二个选择称为内部选择

Get the population of cities which are in Texas
SELECT city, population FROM cities WHERE city IN (SELECT city FROM states WHERE state = 'Texas')
Get employees who have earned more than $500 this week (derived table x)
SELECT name, ROUND(wages) FROM (SELECT name, per_hour * hours_worked AS wages FROM timesheet) AS x WHERE wages > 500
您将使用选择。。。从…起哪里在选择。。。如果您想查找与您所知道的记录相似的记录。可以将内部选择的项目与外部选择的项目进行比较

Get the population of cities which are in Texas
SELECT city, population FROM cities WHERE city IN (SELECT city FROM states WHERE state = 'Texas')
Get employees who have earned more than $500 this week (derived table x)
SELECT name, ROUND(wages) FROM (SELECT name, per_hour * hours_worked AS wages FROM timesheet) AS x WHERE wages > 500
您将使用选择。。。从…起选择如果要为语句创建派生表。然后,可以在外部选择上使用在内部选择上计算的项目

Get the population of cities which are in Texas
SELECT city, population FROM cities WHERE city IN (SELECT city FROM states WHERE state = 'Texas')
Get employees who have earned more than $500 this week (derived table x)
SELECT name, ROUND(wages) FROM (SELECT name, per_hour * hours_worked AS wages FROM timesheet) AS x WHERE wages > 500
还有第三种方法是相关子查询,它将显示为SELECT。。。从…起哪里…=或>或<或!=等选择。。。如果要基于计算选择行,则使用和

Get cities with bigger populations than 2 times the average
SELECT city, population FROM cities WHERE population > 2 * (SELECT AVG(population) FROM cities)

为了回答这个问题,我将第一个选择称为外部选择,第二个选择称为内部选择

Get the population of cities which are in Texas
SELECT city, population FROM cities WHERE city IN (SELECT city FROM states WHERE state = 'Texas')
Get employees who have earned more than $500 this week (derived table x)
SELECT name, ROUND(wages) FROM (SELECT name, per_hour * hours_worked AS wages FROM timesheet) AS x WHERE wages > 500
您将使用选择。。。从…起哪里在选择。。。如果您想查找与您所知道的记录相似的记录。可以将内部选择的项目与外部选择的项目进行比较

Get the population of cities which are in Texas
SELECT city, population FROM cities WHERE city IN (SELECT city FROM states WHERE state = 'Texas')
Get employees who have earned more than $500 this week (derived table x)
SELECT name, ROUND(wages) FROM (SELECT name, per_hour * hours_worked AS wages FROM timesheet) AS x WHERE wages > 500
您将使用选择。。。从…起选择如果要为语句创建派生表。然后,可以在外部选择上使用在内部选择上计算的项目

Get the population of cities which are in Texas
SELECT city, population FROM cities WHERE city IN (SELECT city FROM states WHERE state = 'Texas')
Get employees who have earned more than $500 this week (derived table x)
SELECT name, ROUND(wages) FROM (SELECT name, per_hour * hours_worked AS wages FROM timesheet) AS x WHERE wages > 500
还有第三种方法是相关子查询,它将显示为SELECT。。。从…起哪里…=或>或<或!=等选择。。。如果要基于计算选择行,则使用和

Get cities with bigger populations than 2 times the average
SELECT city, population FROM cities WHERE population > 2 * (SELECT AVG(population) FROM cities)