MySQL where sub-query

Source: Internet
Author: User

A where subquery is the result of an inner query as a comparison condition for the outer layer of SQL.

Select goods_id, goods_name from goods where goods_id = (select Max (goods_id) from goods);

The inner query is select MAX (goods_id) from goods

The outer query is select goods_id, goods_name from goods where goods_id

is to assign the value of the largest goods_id to the outer goods_id.


You can also do this with the comparison operator ' in ':

Select goods_id, cat_id, goods_name from goods where goods_id in (select Max (goods_id) from goods Group by cat_id);

' In ' means that within a set, that is, the largest goods_id in a class grouped by cat_id is assigned to the outer goods_id, so that the maximum goods_id per group can be obtained.



MySQL where sub-query

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.