这篇文章给大家介绍mysql中怎么实现exists子查询,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

 子查询可以分为: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

关于mysql中怎么实现exists子查询就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。