Easy to Know SQL: Volume 1

The database is MS SQL Server 2012, and the data used is the test data in the product account set. The meanings of the example fields are as follows

XSDD sales order CGDD1 purchase order DJLX Document Type
XSDDMX Sales order details CGDD2 Purchase order details YWRQ business date
DDLS Order flow LSBH serial number FKKHMC Paying customer name

Joint query and left and right joins

1. Cross connection

SELECT * FROM XSDD,XSDDMX WHERE XSDD_DDLS = '51'

XSDDMX has a total of 4738 lines, and XSDD has a total of 2803 lines. The result of the above query is 4738 rows, which is actually the product of the column whose DDLS is 51 and the columns of XSDDMX.

This commonly used connection method is called implicit cross connection, and another method that changes the comma between the two tables to cross on is called explicit.

Two, inner connection

SELECT * FROM XSDD,XSDDMX WHERE XSDD_DDLS = XSDDMX_DDLS

Similarly, there are explicit query methods, the results of these two methods are the same, and both return the data rows in the connection table that meet the connection conditions and query conditions:

SELECT * FROM XSDD inner join XSDDMX on XSDD_DDLS = XSDDMX_DDLS

3. Outer connection

Left outer join:

SELECT XSDD_DDLS,CGDD1_LSBH FROM XSDD LEFT OUTER JOIN CGDD1 ON XSDD_DDLS = CGDD1_LSBH

XSDD has a total of 2803 lines, and CGDD1 has a total of 1587 lines. The query result is 2803 lines. You can see that the query result contains all the serial numbers of XSDD. For the part that does not correspond to it in CGDD1, the column of CGDD1 is displayed as NULL. The result is as follows As shown on the left

Right outer join:

SELECT XSDD_DDLS,CGDD1_LSBH FROM XSDD RIGHT OUTER JOIN CGDD1 ON XSDD_DDLS = CGDD1_LSBH

The query result is 1587 lines, and the query result is the right picture. In fact, it can be simply summarized as follows: NULL is on the right side of LEFT, and NULL is on the left side of RIGHT

    

Keywords: Group by

This is a very commonly used keyword, but at the beginning, due to the lack of proficiency in this function, some problems affecting efficiency occurred.

Its application actually focuses on functions such as statistics:

INSERT INTO XSFXHZ 
SELECT XSFXMX_DJLX,'KH',XSFXMX_FKKH,XSFXMX_FKKHMC,'93',ISNULL(sum(XSFXMX_HSJE),0) , 
       XSFXMX_ZGBH FROM XSFXMX 
WHERE  XSFXMX_YWRQ BETWEEN CONVERT(varchar(8),@year)+@s3sta AND          
       CONVERT(varchar(8),@year)+@s3end
GROUP  BY XSFXMX_DJLX,XSFXMX_ZGBH,XSFXMX_FKKH,XSFXMX_FKKHMC

The field to be queried here must be a fixed value, combined with the grouping function, we can conclude that these fields can actually be divided into three types,

  • The first is a ready-made string, such as ' KH ', ' 93 ' here
  • The second is the field to be grouped and queried, such as XSFXMX_DJLX
  • The third is the aggregation function, which calculates the calculation result value of the corresponding computable column in this group (in many cases, it is sum or max, etc.)

For the fields of Group By, they are connected by commas. In the query results, if one of these fields is different, it will form a group independently. Specifically, there are several different groups. This is actually a question of permutation and combination.

Keywords: union and union all 

The function is to merge the result sets of the two queries, and the name of the result field is subject to the result of the first query

SELECT CGDD1_LSBH FROM CGDD1
UNION 
SELECT XSDD_DDLS FROM XSDD

 The difference between the two is that union all does not remove duplicates

SELECT CGDD1_LSBH FROM CGDD1
UNION ALL
SELECT XSDD_DDLS FROM XSDD

function

The content of the function is saved in the database after execution, so it can be called directly in the statement or stored procedure after execution.

In SQL, functions are divided into the following three categories:

  • Built-in functions: provided by the system and cannot be modified
  • Scalar functions: user-defined functions that return a single value
  • Table-valued function: returns a table value

This time, the table-valued function is used in the project, which is written like this

if exists(select * from sys.objects where name = 'GetDwqxFun') drop function GetDwqxFun;
go
CREATE FUNCTION GetDwqxFun(@zgid nvarchar(30))
RETURNS @TempDwlb TABLE 
(
	id nvarchar(30)
)
AS
BEGIN
    --
    INSERT INTO @TempDwlb 
    SELECT @zgid,ZWZGZD_ZGMC FROM ZWZGZD
END 

Pay attention to the specific format, just remember to assign a value to the return table in the function value.

Other commonly used built-in functions

getDate()

The function of this function is to obtain the current system date, and the effect is as follows

SELECT GETDATE() now

Convert()

The role of this function is to convert the date into a general function of the new data type. Can be used together with the one above, the effect is as follows:

SELECT CONVERT(varchar(100), GETDATE(), 112)

 

SQL, which is often used by people as a tool, is completely simple and convenient to operate when it is only used to implement business functions at work. This may be the reason why we quickly forget it when we don't use it.

 

 

Guess you like

Origin blog.csdn.net/qq_41809961/article/details/103365939