SQL statement skills-simple implementation of SQL statements with complex logic (1)

Source: Internet
Author: User

Recently, my colleagues in the Company Met a requirement to implement a function and want to use an sq statement. He asked me to see how to implement SQL. Since we talked about minimizing interaction with databases during the early stage of training, we don't need to use two SQL statements if we can extract one SQL statement:

Create   Table   [ DBO ] . [Zping.com ] (
[ ID ]   [ Varchar ] ( 32 ) Not   Null ,
[ Objid ]   [ Varchar ] ( 32 ) Not   Null ,
[ Objtype ]   [ Tinyint ]   Null ,
[ Config ]   [ Varchar ] ( 256 )
)

Specific requirements:

Table[Zping.com]Data storage: The ID is unique, the objid is the organization ID, The objtype is the organization type, and the config field is

Specific business data.

The ID of the organization where the objid is stored (the ID of the company, the Branch, the office, and the Representative Office) can only be one. The objtype stores the number of the organization.

Yes (the headquarters is 1, the Branch is 2, the Office is 3, and the representative office is 4)

The data in the database may have or may not have all four IDs, or only several of them are available.

 

Business Requirements: the front-end incoming organization ID (Company, Branch, Office, representative office ID), only one data entry can be returned

Ask if there is a company, take out the company, if there is no company, if there is a branch, take the branch... if there is no, then do not take.

 

Final Implementation:
Select   Top   1   [ Config ]   From ( Select   *   From   [ Selfcustom ]   Where Objid In ( ' 66 ' , ' 77 ' ))
T Order   By Objtype ASC

 

Summary:

Here, the order by sorting function is mainly used to achieve that if there is a company, it will go to the company, if there is a company, it will go to the branch. You can use top to retrieve up to one entry.

 

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.