当前位置: 首页 > news >正文

网站顶部地图代码怎么做一键免费创建论坛网站

网站顶部地图代码怎么做,一键免费创建论坛网站,网站速度慢如何做优化,厦门国外网站建设公司哪家好文章目录 MySQL高级语句older by 排序区间判断查询或与且(or 与and)嵌套查询(多条件)查询不重复记录distinctcount 计数限制结果条目limit别名as常用通配符嵌套查询(子查询)同表不同表嵌套查询还能用于删除…

文章目录

  • MySQL高级语句
    • older by 排序
    • 区间判断查询
    • 或与且(or 与and)
    • 嵌套查询(多条件)
    • 查询不重复记录distinct
    • count 计数
    • 限制结果条目limit
    • 别名as
    • 常用通配符
    • 嵌套查询(子查询)
      • 同表
      • 不同表
      • 嵌套查询还能用于删除
      • 嵌套查询还可以用于修改表格
  • 视图
  • 内连接 左连接 右连接
    • 内连接
    • 左连接
    • 右连接
  • 结语

MySQL高级语句

1构建测试用表

create table test1 (id int primary key,name char(30) not null,gpa decimal(3,2),hobbid int(2));
insert into test1 values(1,'baijiahe',3.3,2);
insert into test1 values(2,)'Hu Zexing',2.9,2);
....

在这里插入图片描述

older by 排序

1.order by 排序默认升序

select * from test1 order by gpa;

在这里插入图片描述
2.添加desc 选线可以降序排序

select * from test1 order by gpa desc;

在这里插入图片描述
3.order by 还可以使用where来过滤数据

select * from test1 where hobbid=4   order by gpa desc;	

在这里插入图片描述
4.order by 还可以多个数值一起排序

select * from test  order by gpa,hobbid,id desc;

上面表示按绩点(gpa)排名,分数相同的按照hobbid来序排序hobbid相同按照 id降序排序
在这里插入图片描述

select * from test order by gpa desc,hobbid desc,id desc; 

在这里插入图片描述

区间判断查询

查询GPA大于2.7的数据

select * from test1 where gpa>2.7;

在这里插入图片描述

或与且(or 与and)

查询GPA>2.7或者id=7的数据

select * from test1 where gpa>2.7 or id=7;

在这里插入图片描述

查询GPA>2.7且GPA<=3.2的数据

select * from test1 where gpa>2.7 and gpa<=3.2;

在这里插入图片描述

嵌套查询(多条件)

查找gpa>2.4且(id大于4或者hobbid=2)的数据

select * from test1 where gpa>2.4 and(id>4 or hobbid=2);

在这里插入图片描述

查询不重复记录distinct

select distinct hobbid from test;

查询test表格中 hobbid项中不重复的数据
在这里插入图片描述

count 计数

统计一个有多少数据(包括空行),count(主键名)则不包括空行

select count(*) from test1;

在这里插入图片描述

统计表中hobbid不同的个数

select count(distinct hobbid) from test1;

在这里插入图片描述

限制结果条目limit

注:本文忽略第0行

显示表至第二行

select * from test1 limit 2;

在这里插入图片描述
从第二行开始显示,至第五行

select * from test1 limit 2,5

在这里插入图片描述
查询前五

select * from test1 order by gpa desc limit 1,5;

在这里插入图片描述

别名as

使用别名显示

select name 名字, gpa 绩点 from test1;

在这里插入图片描述
创建表别名

create table t1 as select * from test1;

在这里插入图片描述
用别名查询

select name,gpa from t1;

在这里插入图片描述

常用通配符

1.% - 通配符,匹配任何数量的字符。
2._ - 通配符,匹配任何单个字符。
例如

mysql> SELECT * FROM t1 AS t WHERE t.name LIKE 'w%';

在这里插入图片描述

SELECT * FROM test1 WHERE gpa LIKE '3___';

嵌套查询(子查询)

创建一个新表,用作实验

create table money(id int primary key,money decimal(15,2));
insert into money values (1,3000);
insert into money values (2,5000);
insert into money values (3,1300);
insert into money values (4,6000);
insert into money values (5,2800);
insert into money values (6,2300);
insert into money values (7,3300);
insert into money values (8,9200);
insert into money values (10,3154);
insert into money values (11,31575);
insert into money values (12,8547);
insert into money values (13,8745);
insert into money values (14,900);

在这里插入图片描述

同表

select id,gpa from test1 where id in (select id from test1 where gpa=>2.7)

上面这个SQL语句含义是,首先运行括号内的SQL语句,输出运行结果到达括号外的SQL语句,最为条件运行。
可以理解为上个SQL语句效果相当于SELECT id, gpa FROM test1 WHERE id=1 OR id=2 OR id=3 OR id=4 OR id=5 OR id=9;
在这里插入图片描述
在这里插入图片描述

不同表

select id,money from money where id in (select id  from test1);

在这里插入图片描述

上面这个SQL语句含义是,首先运行括号内的SQL语句,输出运行结果到达括号外的SQL语句,最为条件运行。
可以理解为上个SQL语句效果相当于select id,money from money where id=6 or id=7 or id=8 or id=9 or id=10;

嵌套查询还能用于删除

创建一个测试用的表test2
用select查询是否是要删除的值
是直接将select换成delete就可以删除了

create table test2 (select * from test1);
select * from text2 where id in (select id  from test1 where gpa<=2.7);
delete * from text2 where id in (select id  from test1 where gpa<=2.7);

在这里插入图片描述

嵌套查询还可以用于修改表格

将money表的id=1的用户money数改为1000

update money set money='1000' where id in(select id from test1 where id=1);

在这里插入图片描述

子查询,别名as
当运行

select id from (select id,name from test1);

在这里插入图片描述

会报错
原因为:
select * from 表名 此为标准格式,而以上的查询语句,“表名"的位置其实是一个完整结果集,mysql并不能直接识别,而此时给与结果集设置一个别名,以”select a.id from a“的方式查询将此结果集视为一张"表”,就可以正常查询数据了,如下:

select a.id from (select id,name from test) a;

在这里插入图片描述

相当于
select test.id,name from test;
select 表.字段,字段 from 表;

在MySQL中,视图(View)是一种虚拟表,它由一个SQL查询定义,但并不存储数据。视图的本质是一条SQL查询语句,它的结果集看起来像是一张表。视图可以用来简化复杂的SQL操作,提供数据的安全性和独立性,以及为用户提供一个更加直观的数据表示。

视图

在MySQL中,视图(View)是一种虚拟表,它由一个SQL查询定义,但并不存储数据。视图的本质是一条SQL查询语句,它的结果集看起来像是一张表。视图可以用来简化复杂的SQL操作,提供数据的安全性和独立性,以及为用户提供一个更加直观的数据表示。

CREATE VIEW 试图名 AS SELECT 字段1, 字段2, ... FROM 表名 WHERE 判断句;
create view test_view as select name,gpa from test1 where id<=10;

使用视图

select * from test_view;

在这里插入图片描述
查看视图与源表结构

desc test_view;

在这里插入图片描述

修改视图

update test_view set gpa=3.9 where name=Shirakanga ;

将视图的Shirakanga的gpa改成3.9原表也会自动修改

在这里插入图片描述

内连接 左连接 右连接

内连接

内连接是SQL中的一种连接查询,用于合并两个或多个表中具有匹配值的行。只有当连接条件在两个表中都匹配时,内连接才会返回这些行,内连接只返回两个表中都有匹配的记录的结果集。

SELECT test1.id, test1.name, money.id, money.money FROM test1 INNER JOIN money ON test1.id = money.id;

在这里插入图片描述

左连接

左连接是一种SQL连接操作,它返回左表(LEFT JOIN子句之前的表)的所有记录,即使右表中没有匹配的记录。如果右表中有匹配的记录,则左连接还会返回右表中的匹配记录。如果右表中没有匹配的记录,则结果集中右表的部分将包含NULL。

select * from test1 left join money on test1.id=money.id;

在这里插入图片描述

右连接

右连接(Right Join)是一种SQL连接操作,它返回右表(RIGHT JOIN子句之后的表)的所有记录,即使左表中没有匹配的记录。如果左表中有匹配的记录,则右连接还会返回左表中的匹配记录。如果左表中没有匹配的记录,则结果集中左表的部分将包含NULL。

SELECT * FROM test1 RIGHT JOIN money ON test1.id = money.id;

在这里插入图片描述

结语

  1. ORDER BY 排序:
    • 升序排序:ORDER BY column_name
    • 降序排序:ORDER BY column_name DESC
    • 多条件排序:ORDER BY column1, column2 DESC
  2. 区间判断查询:
    • 查询满足特定条件的数据:WHERE column_name BETWEEN value1 AND value2
  3. 或与且(OR 与 AND):
    • 组合多个条件进行查询:WHERE column_name = 'value' OR column_name2 = 'value2'
    • 组合多个条件进行查询:WHERE column_name = 'value' AND column_name2 = 'value2'
  4. 查询不重复记录(DISTINCT):
    • 去除重复记录:SELECT DISTINCT column_name FROM table_name
  5. COUNT 计数:
    • 统计记录数:SELECT COUNT(*) FROM table_name
  6. 限制结果条目(LIMIT):
    • 限制结果数量:LIMIT offset, limit_number
  7. 别名(AS):
    • 为列设置别名:SELECT column_name AS alias_name FROM table_name
    • 为表设置别名:SELECT * FROM table_name AS alias_name
  8. 常用通配符:
    • %:匹配任何数量的字符。
    • _:匹配任何单个字符。

文章转载自:
http://dinncorelatival.tqpr.cn
http://dinncospinulated.tqpr.cn
http://dinncooutlying.tqpr.cn
http://dinncophenoxide.tqpr.cn
http://dinncocookhouse.tqpr.cn
http://dinncouncaused.tqpr.cn
http://dinncochipmuck.tqpr.cn
http://dinncocutlet.tqpr.cn
http://dinncosmallage.tqpr.cn
http://dinncoempiristic.tqpr.cn
http://dinncoblooming.tqpr.cn
http://dinncoketogenic.tqpr.cn
http://dinncoanapest.tqpr.cn
http://dinncofrontlet.tqpr.cn
http://dinncooften.tqpr.cn
http://dinncorightpages.tqpr.cn
http://dinncothready.tqpr.cn
http://dinncoalban.tqpr.cn
http://dinncosabbath.tqpr.cn
http://dinncouxoricide.tqpr.cn
http://dinncocribrose.tqpr.cn
http://dinncolotto.tqpr.cn
http://dinncospringwater.tqpr.cn
http://dinncosonderkommando.tqpr.cn
http://dinncoquestura.tqpr.cn
http://dinncodelectus.tqpr.cn
http://dinncohypospadias.tqpr.cn
http://dinnconehemias.tqpr.cn
http://dinncoguam.tqpr.cn
http://dinncoamputation.tqpr.cn
http://dinncodybbuk.tqpr.cn
http://dinncoxanthochroi.tqpr.cn
http://dinncoanemophily.tqpr.cn
http://dinncochasmic.tqpr.cn
http://dinncopartly.tqpr.cn
http://dinncofootwork.tqpr.cn
http://dinncophlebosclerosis.tqpr.cn
http://dinncobaume.tqpr.cn
http://dinncobutt.tqpr.cn
http://dinncohollywoodize.tqpr.cn
http://dinncooscillate.tqpr.cn
http://dinncotartness.tqpr.cn
http://dinncosapiency.tqpr.cn
http://dinncoknotless.tqpr.cn
http://dinncogruntle.tqpr.cn
http://dinncopsych.tqpr.cn
http://dinncoacetylase.tqpr.cn
http://dinncosupervisor.tqpr.cn
http://dinncoharl.tqpr.cn
http://dinncoagrochemical.tqpr.cn
http://dinnconesselrode.tqpr.cn
http://dinncogeographer.tqpr.cn
http://dinncoliliaceous.tqpr.cn
http://dinncochartered.tqpr.cn
http://dinncoglobose.tqpr.cn
http://dinncowedeln.tqpr.cn
http://dinncocollie.tqpr.cn
http://dinncomiai.tqpr.cn
http://dinncocoolabah.tqpr.cn
http://dinncohomostylous.tqpr.cn
http://dinncolongsome.tqpr.cn
http://dinncoshaving.tqpr.cn
http://dinncohoundfish.tqpr.cn
http://dinncosalivate.tqpr.cn
http://dinncoenvious.tqpr.cn
http://dinncofiliopietistic.tqpr.cn
http://dinncohidrotic.tqpr.cn
http://dinncopromise.tqpr.cn
http://dinncowheresoever.tqpr.cn
http://dinncodisemplane.tqpr.cn
http://dinncoserving.tqpr.cn
http://dinncoaxiologist.tqpr.cn
http://dinncousefulness.tqpr.cn
http://dinncoacrogenous.tqpr.cn
http://dinncodead.tqpr.cn
http://dinncounverbalized.tqpr.cn
http://dinncotutania.tqpr.cn
http://dinncoexactness.tqpr.cn
http://dinncostephanotis.tqpr.cn
http://dinncocomminute.tqpr.cn
http://dinncopodolsk.tqpr.cn
http://dinncowise.tqpr.cn
http://dinncomazda.tqpr.cn
http://dinncomenisci.tqpr.cn
http://dinncoanimatism.tqpr.cn
http://dinncouranite.tqpr.cn
http://dinncoweld.tqpr.cn
http://dinncoepistoma.tqpr.cn
http://dinncopancuronium.tqpr.cn
http://dinncorunoff.tqpr.cn
http://dinncochardin.tqpr.cn
http://dinncophytomer.tqpr.cn
http://dinncozohar.tqpr.cn
http://dinncoswimsuit.tqpr.cn
http://dinncoeleventh.tqpr.cn
http://dinncocasually.tqpr.cn
http://dinncoboned.tqpr.cn
http://dinncopatelliform.tqpr.cn
http://dinncodendriform.tqpr.cn
http://dinncovolucrary.tqpr.cn
http://www.dinnco.com/news/138138.html

相关文章:

  • 婚礼网站怎么做怎么做网站推广和宣传
  • 乐清手机网站营销推广与策划
  • 商城网站建设信息天津百度网站快速排名
  • 迷糊娃 wordpress 主题福州网站优化
  • 网站升级 html青岛百度seo排名
  • 微信公众号如何分享wordpressseo短视频网页入口引流
  • 如何做英文网站seo什么意思
  • 江阴网站制作新闻播报最新
  • 视频网站如何做微信营销媒体:多地新增感染趋势回落
  • 网站月付服务器怎么制作小程序
  • 大学生网页设计个人主页优化seo可以从以下几个方面进行
  • 北京 集团公司网站建设培训体系搭建
  • 网站建设需要会什么软件专业seo培训
  • 网站图片上传不了是什么原因百度官网链接
  • 织梦学校网站源码网络优化培训要多少钱
  • 泗泾做网站西安seo顾问
  • wordpress模板调用数据合肥网络推广优化公司
  • 杭州外贸网站建设公司seo推广案例
  • 长沙的网站制作公司全球疫情今天最新消息
  • 新闻网站开发课程设计模板百度seo公司哪家好一点
  • 网站备案 身份证水印如何推广微信公众号
  • 做网站有哪些项目如何提高网站搜索排名
  • 嘉定企业网站开发建设提高百度搜索排名
  • 网站制作与建立百度免费推广网站
  • 广州市网络广告推广企业seo实战培训视频
  • 郑州网站制作-中国互联潍坊网站排名提升
  • 怎么让百度多收录网站百度推广业务电话
  • 网站数据库怎么恢复企业培训方案制定
  • 做网站需要会写代码惠州seo外包平台
  • 做网站看什么书好学seo网络推广