Oracle 5.0 Reference Manual page 1119

Table of Contents

Advertisement

In MySQL, you cannot modify a table and select from the same table in a subquery. This applies to
statements such as DELETE, INSERT, REPLACE, UPDATE, and (because subqueries can be used in
the
clause)
SET
For information about how the optimizer handles subqueries, see
Subqueries with
performance issues for certain forms of subquery syntax, see
Subqueries".
13.2.9.1. The Subquery as Scalar Operand
In its simplest form, a subquery is a scalar subquery that returns a single value. A scalar subquery is a
simple operand, and you can use it almost anywhere a single column value or literal is legal, and you
can expect it to have those characteristics that all operands have: a data type, a length, an indication
that it can be NULL, and so on. For example:
CREATE TABLE t1 (s1 INT, s2 CHAR(5) NOT NULL);
INSERT INTO t1 VALUES(100, 'abcde');
SELECT (SELECT s2 FROM t1);
The subquery in this
length of 5, a character set and collation equal to the defaults in effect at
an indication that the value in the column can be NULL. Nullability of the value selected by a scalar
subquery is not copied because if the subquery result is empty, the result is NULL. For the subquery
just shown, if
There are a few contexts in which a scalar subquery cannot be used. If a statement permits only a
literal value, you cannot use a subquery. For example,
LOAD DATA INFILE
values.
When you see examples in the following sections that contain the rather spartan construct
column1 FROM
constructions.
Suppose that we make two tables:
CREATE TABLE t1 (s1 INT);
INSERT INTO t1 VALUES (1);
CREATE TABLE t2 (s1 INT);
INSERT INTO t2 VALUES (2);
Then perform a SELECT:
SELECT (SELECT s1 FROM t2) FROM t1;
The result is
A scalar subquery can be part of an expression, but remember the parentheses, even if the subquery is
an operand that provides an argument for a function. For example:
SELECT UPPER((SELECT s1 FROM t1)) FROM t2;
13.2.9.2. Comparisons Using Subqueries
The most common use of a subquery is in the form:
non_subquery_operand comparison_operator
Where
comparison_operator
=
>
<
>=
LOAD DATA
INFILE.
Strategy". For a discussion of restrictions on subquery use, including
EXISTS
returns a single value ('abcde') that has a data type of CHAR, a
SELECT
were empty, the result would be
t1
requires a literal string file name. You cannot use subqueries to supply these
t1), imagine that your own code contains much more diverse and complex
because there is a row in
2
is one of these operators:
<=
<>
!=
<=>
Subquery Syntax
even though
NULL
LIMIT
containing a column
t2
(subquery)
1099
Section 8.3.1.14, "Optimizing
Section E.3, "Restrictions on
CREATE TABLE
is
s2
NOT
NULL.
requires literal integer arguments, and
that has a value of 2.
s1
time, and
(SELECT

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents