SQL code Example Analysis

Source: Internet
Author: User

1. Conditional Filtering & having

Table structure

#tab_a

#tab_b

Table relationships

Tab_a.id = tab_b.relation_id

Table Data

Demand

Inquiry the Tab_a ID corresponding to the table Tab_b member_id, when not found in table Tab_b, assignment is null

SelectTab_a.id, Case       when(SELECT Count(member_id) fromTab_bWHERErelation_id=Tab_a.id)= 0  Then NULL      ELSE(SELECTmember_id fromTab_bWHERErelation_id=tab_a.id)End  asresult fromTab_a

Results

Problem

When the ID of tab_a and the member_id of Tab_b exist 1-to-many relationships, such as Tab_b

Making the above SQL query again is an error:

[ERR] ERROR: More  than one row returned by a subquery used as an expression

Problem

How do I find the ID in tab_a corresponding to the tab_b in member_id there are multiple?

Select Count (*fromwhere tab_a.id=Groupby havecount (*) > 1

Results

  

SQL code Example Analysis

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.