共計(jì) 754 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
這篇文章給大家介紹 mysql 中怎么實(shí)現(xiàn) exists 子查詢,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
子查詢可以分為:from where 和 exists 子查詢
分類表:
[sql]
mysql select * from category;
+—-+———+
| id | c_name |
+—-+———+
| 1 | ios |
| 2 | android |
| 3 | sb |
+—-+———+
3 rows in set
商品表:
[sql]
mysql select * from goods;
+—-+———+——–+——-+—–+
| id | name | cat_id | price | num |
+—-+———+——–+——-+—–+
| 1 | 蘋果 | 1 | 4999 | 2 |
| 2 | nexus4 | 2 | 1999 | 3 |
| 4 | 榮耀 2 | 2 | 1888 | 5 |
| 6 | 三星 | 2 | 3000 | 2 |
+—-+———+——–+——-+—–+
6 rows in se
要求:只找出分類下有商品的分類
[sql]
mysql select * from category c where exists (select * from goods where cat_id=c.id);
+—-+———+
| id | c_name |
+—-+———+
| 1 | ios |
| 2 | android |
+—-+———+
2 rows in set
關(guān)于 mysql 中怎么實(shí)現(xiàn) exists 子查詢就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。