T-SQL Query Basics

1. Query some columns in the data table

SELECT  AuthorName, Age, Sex, City
FROM    Authors

Second, use "*" to find all columns

SELECT  *   FROM  Authors

3. Use WHERE to filter some rows of data

SELECT  AuthorName, Age, Sex, City
FROM    Authors
WHERE   City='北京'

Fourth, use the As clause to alias the column

SELECT AuthorName as '作者姓名', Email as 电子邮件, Age as '年龄'
FROM   Authors

You can also use "=" to convert aliases

SELECT '作者姓名'=AuthorName, 电子邮件=Email, 年龄=Age
FROM   Authors

5. Use "+" to connect multiple fields and combine them into one column

SELECT AuthorName +'   '+ City as 作者居住城市
FROM   Authors

6. Use the Top keyword to query the limited number of rows in the table

SELECT TOP 5 *  FROM  Authors 

7. Use the DISTINCT keyword to block duplicate data

SELECT DISTINCT City FROM Authors

Eight, use IS NULL to query empty data

SELECT AuthorName, Birthday, City
FROM   Authors
WHERE  Birthday IS NULL

Task 1: How to determine the order within a month?

First, we stipulate that a month is 30 days.

Second, determine orders within 30 days: current date - order date < 30

You need to use the date function DATEDIFF to calculate the difference between dates.

SELECT OrderID as 订单编号, OrderDate as 订购日期,
              CustomerID as 客户编号, Total as 总费用
FROM Orders
WHERE DATEDIFF(dd, GETDATE(), orderDate) <=30

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325294256&siteId=291194637