Mysql own practice

Conditions :( single query)

1, the query is a7c9bff53f2e4a70af7a9f641552507a Table B user_id field inside the wave (two tables related query) (left connection query conditions)

SELECT a.id,b.wave_id,b.single_wave_length FROM `wave_origin_data` a LEFT JOIN 

eigen_value_one b ON a.id=b.wave_id where user_id="a7c9bff53f2e4a70af7a9f641552507a";

2, Nested SELECT statement also called sub-query

Subqueries essentially nested into other SELECT, a restricted SELECT statement UPDATE, INSERT, DELETE statement, in the sub-query, only a few clauses can use the following

  1. SELECT clause (must)
  2. FROM clause (mandatory)
  3. WHERE clause (optional)
  4. GROUP BY (optional)
  5. HAVING (optional)
  6. ORDER BY (only to be used if available in the TOP keyword)
子查询位于from之后的


SELECT     P.ProductID, P.Name, P.ProductNumber, M.Name AS ProductModelName
FROM         Production.Product AS P INNER JOIN
(SELECT     Name, ProductModelID
 FROM          Production.ProductModel) AS M 
ON P.ProductModelID = M.ProductModelID

Subquery as a condition of

我想取得总共请病假天数大于68小时的员工:

SELECT [FirstName]
      ,[MiddleName]
      ,[LastName]
  FROM [AdventureWorks].[Person].[Contact]
  WHERE ContactID IN 

  (SELECT EmployeeID
  FROM [AdventureWorks].[HumanResources].[Employee]
  WHERE SickLeaveHours>68)

Select subquery

SELECT [Name],
      (SELECT COUNT(*) FROM AdventureWorks.Sales.SalesOrderDetail S
      WHERE S.ProductID=P.ProductID) AS SalesAmount
FROM [AdventureWorks].[Production].[Product] P
Group by子查询
SELECT P.Name,COUNT(S.ProductID)
FROM [AdventureWorks].[Production].[Product] P 
LEFT JOIN  AdventureWorks.Sales.SalesOrderDetail S
ON S.ProductID=P.ProductID
GROUP BY P.Name
  

Reference Address: https://www.cnblogs.com/CareySon/archive/2011/07/18/2109406.html

 

All statements sql query keywords :( basis for comparison)

Reference Address: https://blog.csdn.net/hundan_520520/article/details/54881234

Sort Test:

Query the database tables inside the blood pressure values ​​of the day:

SELECT high_pressure,low_pressure  FROM `report_disease` WHERE 
disease_code=14 AND date(start_time) = curdate() AND user_id='e96dbbdc5d764832afaa8ded676bf15a';

COUNT (*) function returns the number of rows in a given selection is selected.

语法:SELECT  COUNT(*) FROM  table

count (*) includes all of the columns, equivalent to the number of rows in the statistics, it does not ignore the column value is NULL

count (htt) represents the number of the emergence of this value is not null of htt.

 

2019-6-27 test:

Table join queries, subqueries averaging:

SELECT AVG(t.single_wave_length) FROM(SELECT a.id,b.wave_id,b.single_wave_length 
FROM `wave_origin_data` a LEFT JOIN eigen_value_one b ON a.id=b.wave_id where user_id="e96dbbdc5d764832afaa8ded676bf15a") AS t;

Comparison operation

只显示id,name:     select id,name from students;
消除重复行distinct
select distinct gender from students;


condition

select * from students where id>3;
select * from students where isDelete=0;


logic operation

and
or
not
select * from students where id>3 and gender=0;
select * from students where id<4 or isDelete=0;


Fuzzy query
like
%: represents any number of characters
_: represents an arbitrary character
query surname Luo people:

select * from students where name like '罗%';


Queries folks and the name is a word people:

select * from students where name like '杨_';


Xue surname and the name of the query word people:

select * from students where name like '薛__';


Inquiry or call Yan surname Luo people:

select * from students where name like '罗%' or name like '%嫣';


Range queries
in: expressed in a non-continuous range
query number is 1 or 3 or 8 people

select * from students where id in(1,3,8);


BETWEEN ... and ... represents a contiguous range (between decimal places, and the large numbers)
query id is the human 3-8

select * from students where id between 3 and 8;
select * from students where id between 3 and 8 and gender=1;

Analyzing empty
null and '' is not the same
determination: is null

select * from students where birthday is null;
select * from students where birthday is not null;


Priority
parentheses, not, comparison operators, logic operators
and first ratio operation or, if desired and appears to count at the same time or, requires a combination of () using

Guess you like

Origin blog.csdn.net/chehec2010/article/details/92610478