第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

MySQL 子查詢

標(biāo)簽:
MySQL

Summary: in this tutorial, we will show you how to use the MySQL subquery to write complex queries and explain the correlated subquery concept.

A MySQL subquery is a query that is nested inside another query such as SELECTINSERTUPDATE or DELETE. A MySQL subquery is also can be nested inside another subquery. A MySQL subquery is also called an inner query, while the query that contains the subquery is called an outer query.

Let’s take a look at the following subquery that returns employees who locate in the offices in the USA.

  • The subquery returns all offices codes of the offices that locate in the USA.

  • The outer query selects the last name and first name of employees whose office code is in the result set returned from the subquery.

MySQL Subquery

You can use a subquery anywhere an expression can be used. A subquery also must be enclosed in parentheses.

MySQL subquery within a WHERE clause

MySQL subquery with comparison operators

If a subquery returns a single value, you can use comparison operators to compare it with the expression in the WHERE clause. For example, the following query returns the customer who has the maximum payment.

SELECT customerNumber,        checkNumber,        amount FROM payments WHERE amount = ( SELECT MAX(amount)          FROM payments )

mysql subquery with equal operator

You can also use other comparison operators such as greater than (>), less than(<), etc. For example, you can find customer whose payment is greater than the average payment. A subquery is used to calculate the average payment by using the AVG aggregate function. The outer query selects payments that are greater than the average payment returned from the subquery.

SELECT customerNumber,        checkNumber,    amount FROM payments WHERE amount > ( SELECT AVG(amount)      FROM payments )

 

 

mysql subquery with greater than operator

MySQL subquery with IN and NOT IN operators

If a subquery returns more than one value, you can use other operators such as IN or NOT IN operator in the WHEREclause. For example, you can use a subquery with NOT IN operator to find customer who has not ordered any product as follows:

SELECT customername FROM customers WHERE customerNumber NOT IN( SELECT DISTINCT customernumber FROM orders )

 

 

mysql subquery not in

MySQL subquery with EXISTS and NOT EXISTS

When a subquery is used with EXISTS or NOT EXISTS operator, a subquery returns a Boolean value of TRUE or FALSE. The subquery acts as an existence check.

In the following example, we select a list of customers who have at least one order with total sales greater than 10K.

First, we build a query that checks if there is at least one order with total sales greater than 10K:

SELECT priceEach * quantityOrdered FROM orderdetails WHERE priceEach * quantityOrdered > 10000 GROUP BY orderNumber

mysql subquery exists example

The query returns 6 records so that when we use it as a subquery, it will return TRUE; therefore the whole query will return all customers:

SELECT customerName FROM customers WHERE EXISTS (     SELECT priceEach * quantityOrdered     FROM orderdetails     WHERE priceEach * quantityOrdered > 10000     GROUP BY orderNumber )

mysql subquery exists example

If you replace the EXISTS by NOT EXIST in the query, it will not return any record at all.

MySQL subquery in FROM clause

When you use a subquery in the FROM clause, the result set returned from a subquery is used as a table. This table is referred to as a derived table or materialized subquery.

The following subquery finds the maximum, minimum and average number of items in sale orders:

SELECT max(items),        min(items),        floor(avg(items)) FROM (SELECT orderNumber,  count(orderNumber) AS items FROM orderdetails GROUP BY orderNumber) AS lineitems

Notice that the subquery returns the following result set that is used as a derived table for the outer query.

mysql subquery from clause example

MySQL correlated subquery

In the previous examples, we see the subquery itself is independent. It means that you can execute the subquery as a normal query. However a correlated subquery is a subquery that uses the information from the outer query, or we can say that a correlated subquery depends on the outer query. A correlated subquery is evaluated once for each row in the outer query.

In the following correlated subquery, we select products whose buy price is greater than the average buy price of all products for a particular product line.

SELECT productname,        buyprice FROM products AS p1 WHERE buyprice > (  SELECT AVG(buyprice) FROM products         WHERE productline = p1.productline)

MySQL correlated subquery example

The inner query executes for every product line because the product line is changed for every row. Hence the average buy price will also change.

In this tutorial, we have shown you how to use MySQL subquery and correlated subquery to write more complex queries.

原文链接:http://outofmemory.cn/mysql/mysql-subquery

點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費專欄免費學(xué)

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消