Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql subquery result in "where" clause

Tags:

mysql

Is it possible to execute mysql query like this?

select (select A from B where ... ) as C from D where C like ' ... ' 

I need to use the result of subquery in general "where" clause.

like image 636
Eddie Avatar asked Dec 21 '11 13:12

Eddie


People also ask

Can we use subquery in WHERE clause in MySQL?

In MySQL subquery can be nested inside a SELECT, INSERT, UPDATE, DELETE, SET, or DO statement or inside another subquery. A subquery is usually added within the WHERE Clause of another SQL SELECT statement. You can use the comparison operators, such as >, <, or =.

Can I use subquery in WHERE clause?

Subqueries in the WHERE Clause. A subquery in a WHERE clause can be used to qualify a column against a set of rows. For example, the following subquery returns the department numbers for departments on the third floor. The outer query retrieves the names of employees who work on the third floor.

Is subquery not allowed in WHERE clause?

References to columns in the parent query are allowed only in the WHERE clause of the subquery. Subquery predicates that reference a column in a parent query must use the Equals To ( = ) predicate operator. Subquery predicates may not refer only to columns in the parent query.

Can subquery retrieve data from inner query used in WHERE clause?

A subquery cannot contain a BETWEEN or LIKE clause. A subquery cannot contain an ORDER BY clause. A subquery in an UPDATE statement cannot retrieve data from the same table in which data is to be updated. A subquery in a DELETE statement cannot retrieve data from the same table in which data is to be deleted.


2 Answers

You can wrap it in a sub-query like this:

SELECT *  FROM (         select (select A from B where ... ) as C from D      ) subq WHERE      C like ' ... ' 
like image 71
a'r Avatar answered Sep 22 '22 14:09

a'r


Have you read this?

http://dev.mysql.com/doc/refman/5.0/en/subqueries.html

A subquery is a SELECT statement within another statement.

Starting with MySQL 4.1, all subquery forms and operations that the SQL standard requires are supported, as well as a few features that are MySQL-specific.

Here is an example of a subquery:

SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);  

In this example, SELECT * FROM t1 ... is the outer query (or outer statement), and (SELECT column1 FROM t2) is the subquery. We say that the subquery is nested within the outer query, and in fact it is possible to nest subqueries within other subqueries, to a considerable depth. A subquery must always appear within parentheses.

The main advantages of subqueries are:

They allow queries that are structured so that it is possible to isolate each part of a statement.

They provide alternative ways to perform operations that would otherwise require complex joins and unions.

Many people find subqueries more readable than complex joins or unions. Indeed, it was the innovation of subqueries that gave people the original idea of calling the early SQL “Structured Query Language.”

Here is an example statement that shows the major points about subquery syntax as specified by the SQL standard and supported in MySQL:

like image 26
Derk Arts Avatar answered Sep 20 '22 14:09

Derk Arts