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.