Mysql自己练习

条件:(单条查询)

1、查询user_id是a7c9bff53f2e4a70af7a9f641552507a的B表里面字段wave(俩张表联查)(左连接条件查询)

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、嵌套SELECT语句也叫子查询

子查询本质上是嵌套进其他SELECT,UPDATE,INSERT,DELETE语句的一个被限制的SELECT语句,在子查询中,只有下面几个子句可以使用

  1. SELECT子句(必须)
  2. FROM子句(必选)
  3. WHERE子句(可选)
  4. GROUP BY(可选)
  5. HAVING(可选)
  6. ORDER BY(只有在TOP关键字被使用时才可用)
子查询位于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

子查询作为条件使用的

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

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

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

Select子查询

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
  

参考值地址:https://www.cnblogs.com/CareySon/archive/2011/07/18/2109406.html

sql的所有关键字查询的语句:(比较基础)

参考地址:https://blog.csdn.net/hundan_520520/article/details/54881234

排序测试:

查询数据库表里面当天的血压值:

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

COUNT(*)  函数返回在给定的选择中被选的行数。

语法:SELECT  COUNT(*) FROM  table

count(*)包括了所有的列,相当于行数,在统计结果的时候,不会忽略列值为NULL

count(htt)表示htt这个值不是null的出现的次数。

2019-6-27测试:

查询表连接,实现子查询求均值:

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;

比较运算

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


条件

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


逻辑运算

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


模糊查询
like
% : 表示任意多个字符
_ : 表示一个任意字符
查询姓罗的人:

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


查询姓杨 且 名字是一个字的人:

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


查询姓薛 且 名字两个字的人:

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


查询姓罗 或者 叫嫣 的人:

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


范围查询
in :表示在一个非连续的范围内
查询编号是1或3或8的人

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


between ... and ... 表示在一个连续的范围内(between后小数,and后大数)
查询id是3到8的人

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

空判断
null与''不相同
判断: is null

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


优先级
小括号, not, 比较运算符, 逻辑运算符
and比or先运算,如果同时出现并希望先算or,需要结合()使用

猜你喜欢

转载自blog.csdn.net/chehec2010/article/details/92610478
今日推荐