简体   繁体   中英

Sql syntax: select without from clause as subquery in select (subselect)

While editing some queries to add alternatives for columns without values, I accidentally wrote something like this (here is the simplyfied version):

SELECT id, (SELECT name) FROM t

What you within your first query is a correlated subquery which simply returns the name column from the table t . no actual subquery needs to run here (which is what your EXPLAIN is telling you).

In a SQL database query, a correlated subquery (also known as a synchronized subquery) is a subquery (a query nested inside another query) that uses values from the outer query.

https://en.wikipedia.org/wiki/Correlated_subquery

SELECT id, (SELECT name) FROM t

is the same as

SELECT id, (SELECT t.name) FROM t

Your 2nd query

SELECT id, (SELECT name FROM t AS t1 where t1.id=t2.id) FROM t AS t2

Also contains correlated subquery but this one is actually running a query on table t to find records where t1.id = t2.id.

This is the default behavior for the SQL language and it is defined on the SQL ANSI 2011 over ISO\/IEC 9075-1:2011(en)<\/a> documentation. Unfortunately it is not open. This behavior is described on the section 4.11 SQL-Statements.

select id, (select name) from some 

It's not standard. In oracle,

select 1, (select 2) 
from dual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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