通過使用IN關(guān)鍵字,我們可以使用子查詢來過濾數(shù)據(jù)。這是因?yàn)槲覀兛梢韵袷褂弥盗斜硪粯邮褂貌樵兘Y(jié)果,使用IN運(yùn)算符根據(jù)另一個(gè)查詢的結(jié)果來過濾查詢。子查詢出現(xiàn)在IN關(guān)鍵字后的括號(hào)中。
示例
我們將使用以下表格中的數(shù)據(jù)來說明這個(gè)示例 ?
mysql> Select * from Customers; +-------------+----------+ | Customer_Id | Name | +-------------+----------+ | 1 | Rahul | | 2 | Yashpal | | 3 | Gaurav | | 4 | Virender | +-------------+----------+ 4 rows in set (0.00 sec) mysql> Select * from Reservations; +------+-------------+------------+ | ID | Customer_id | Day | +------+-------------+------------+ | 1 | 1 | 2017-12-30 | | 2 | 2 | 2017-12-28 | | 3 | 2 | 2017-12-29 | | 4 | 1 | 2017-12-25 | | 5 | 3 | 2017-12-26 | +------+-------------+------------+ 5 rows in set (0.00 sec)
登錄后復(fù)制
下面的查詢使用了帶有子查詢的‘IN’運(yùn)算符,并在比較子查詢返回的所有值后返回結(jié)果。
mysql> SELECT * from customers WHERE customer_id IN (Select customer_id from reservations); +-------------+----------+ | Customer_Id | Name | +-------------+----------+ | 1 | Rahul | | 2 | Yashpal | | 3 | Gaurav | +-------------+----------+ 3 rows in set (0.00 sec)
登錄后復(fù)制
以上就是如何借助MySQL子查詢來過濾數(shù)據(jù)?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!