Common SQL statements (nested subqueries/randomization, and so on) detailed collation _mssql

Source: Internet
Author: User
1. SQL INSERT statement gets automatically generated increment ID value
Copy Code code as follows:

Insert into Table1 (name,des,num) VALUES (' LTP ', ' thisisbest ', 10);
SELECT @ @identity as ' Id '

2. Implementation is 1 or 0 want to show as male or female
Copy Code code as follows:

Select name,sex= (Case Sex when ' 1 ' Then ' man ' when ' 0 ' then ' female ') from TableName

3. Nested subqueries
Copy Code code as follows:

Select A,b,c from Table1 where a in (select a from Table2)

4. Show article, submitter, and last reply time
Copy Code code as follows:

Select A.title,a.username,b.adddate from TableName A, (select Max (adddate) adddate from TableName where tablename.title=a . title) b

5. Examples of random extraction of strip records
Copy Code code as follows:

SQL Server:select Top * out tablename ORDER by NewID ()

Access:select Top * FROM tablename ORDER by Rnd (ID)

Rnd (ID) The ID is an AutoNumber field, which can be done using any number of other values, such as the name of a segment username

Select Top * FROM Table order by Rnd (Len (UserName))

Mysql:select * FROM Table order by Rand () Limit 10

6. Find a record of the same property in the same table
Copy Code code as follows:

Select UserID from accounts_users where UserName isn't null GROUP by UserID have Count (*) >1

7. Query category all product corresponding data
Copy Code code as follows:

SELECT Categoryname,productname from Categories left JOIN the products on categories.categoryid = Products.CategoryID;

8. Query for user information numbered from 2 to 5 by scope
Copy Code code as follows:

SELECT * from Uservalue where UserID between 2 and 5;

9. Schedule 5 minutes Advance reminder
Copy Code code as follows:

Select * from Tabschedule where DateDiff (Minute,getdate (), start time) <5

10. Arrive at the maximum number of days of the month in which a date
Copy Code code as follows:

SELECT Day (DATEADD (DD,-day (' 2008-02-13 '), DATEADD (mm, 1, ' 2008-02-13 '))) as ' Daynumber '

11. Sort by last name stroke
Copy Code code as follows:

Select * FROM TableName ORDER by CustomerName Collate Chinese_prc_stroke_ci_as

12. Some uses of wildcard characters
Copy Code code as follows:

1.select * FROM TableName where column1 like ' [a-m]% ' to select the record of the first letter in the column field between A-M
2.select * FROM TableName where column1 like ' [abc]% ' so you can select a record with the first letter A or B or C in the column field
3.select * FROM TableName where column1 like ' [a-cg]% ' can select the record of the first letter in the column field between A-c or G
4.select * FROM TableName where column1 like ' [^c]% ' so you can select a record with the first letter not C in the column field

13. Copy table structure (copy structure only, source table name: A, target table name: b)
Copy Code code as follows:

SELECT * into B from a where 1<>1 or select top 0 * to [b] from [a]

14. Copy table data (copy data, source table name: A, destination table name: b)
Copy Code code as follows:

INSERT into B (name,des,num) select Name,des,num from Table1;

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.