mysql之exists子查詢 子查詢可以分為:from where 和 exists子查詢 www.2cto.com 分類表: [sql] mysql> select * from category; +----+---------+ | id | c_name | +----+---------+ | 1 | ios | | 2 | android | | 3 | sb | +----+---------+ 3 rows in set 商品表: www.2cto.com [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 www.2cto.com 要求:只找出分類下有商品的分類 [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