Home > Web Front-end > Front-end Q&A > Is javascript a sql language?

Is javascript a sql language?

青灯夜游
Release: 2022-09-20 15:25:32
Original
1585 people have browsed it

Javascript is not a sql language. JavaScript is a multi-paradigm dynamic scripting language based on prototype programming. It is often used to add various dynamic functions to web pages to provide users with smoother and more beautiful browsing effects; while SQL is a database query and programming language. Used to access data and query, update and manage relational database systems.

Is javascript a sql language?

The operating environment of this tutorial: windows7 system, javascript1.8.5&&mysql8 version, Dell G3 computer.

javascript is not a sql language. Javascript and SQL languages ​​are two different languages, and their uses are also different.

What is javascript?

JavaScript ("JS" for short) is a lightweight, interpreted or just-in-time compiled programming language with function priority. Although it is famous as a scripting language for developing Web pages, it is also used in many non-browser environments. JavaScript is a dynamic scripting language based on prototype programming, multi-paradigm, and supports object-oriented, imperative and declarative (such as functional programming) style.

JavaScript is a high-level scripting language that belongs to the Internet. It has been widely used in Web application development. It is often used to add various dynamic functions to web pages and provide users with smoother and more beautiful browsing effects. Usually JavaScript scripts realize their functions by embedding them in HTML.

Main functions

1. Embed dynamic text in HTML pages.

2. Respond to browser events.

3. Read and write HTML elements.

4. Validate data before it is submitted to the server.

5. Detect the visitor’s browser information. Control cookies, including creation and modification.

6. Server-side programming based on Node.js technology.

How to use JavaScript

The type attribute of script does not need to be written. If you want to write it, use

type="text/javascript"
Copy after login

Method 1: Internal method of js

<script>
//单行注释
/*多行注释*/
//在js常见的函数
//向浏览器打印内容,类似于Java中的控制台输出语句
document.write("hello,JavaScript我来了!") ;
//还可以在浏览器中控制台中打印内容
console.log("hello,JavaScript我来了") ;
//在浏览中弹出一个提示框  
//window对象是浏览器中顶级对象,可以省略不写!
//window.alert("helloworld") ;
//简写为:
alert("helloworld") ;
</script> -->
Copy after login

Method 2: External method

In actual development (used by front-end developers!)

You need to create a separate file with the suffix .js in the js folder

Write js code and import the js file into the current html page!

Import external js file

<script src="js/01.js"></script>
Copy after login

What is SQL?

Structured Query Language (SQL), referred to as SQL, is a special-purpose programming language. It is a database query and programming language used for accessing data and querying. , update and manage relational database systems.

SQL can be functionally divided into three parts: data definition, data manipulation and data control.

  • 1. SQL data definition function: Able to define the three-level schema structure of the database, namely external schema, global schema and internal schema structure. In SQL, the external schema is also called a view, and the global schema is referred to as a schema. The internal schema is automatically implemented by the system based on the database schema, and generally does not require user intervention.

  • 2. SQL data manipulation functions: including data insertion, deletion and modification of basic tables and views, especially strong data query functions.

  • 3. SQL data control function: mainly controls user access rights to ensure system security.

DDL statement: database operation statement

DDL statement of the database (database definition statement) library operation

-- :普通注释 当行注释
/* mysql的多行注释*/
#特殊注释
-- 查询当前mysql中自带的所有库有哪些
库在我们电脑磁盘上----> 文件夹
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |   mysql的默认配置库
| mysql              |   有user表 (管理员用户表) :root用户就在这个库中
| performance_schema |   mysql其他库(性能相关)
| test               |   测试库,但是不用它,自己创建新的库
+--------------------+
4 rows in set (0.00 sec)

-- 创建库
-- create database 库名;
mysql> create database myEE_2203 ;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| myee_2203          |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)


-- create database if not exists 库名;
mysql> create database if not exists  ee2203;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ee2203             |
| myee_2203          |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)


-- 查询创建库的字符集
-- show create database  库名;
mysql> show create database myee_2203;
+-----------+--------------------------------------------------------------------+
| Database  | Create Database                                                    |
+-----------+--------------------------------------------------------------------+
| myee_2203 | CREATE DATABASE `myee_2203` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+-----------+--------------------------------------------------------------------+
1 row in set (0.00 sec)

-- 修改库的字符集
-- alter database 库名 default character set 字符集名称;
mysql> alter database myee_2203 default character set gbk;
Query OK, 1 row affected (0.00 sec)
mysql> show create database myee_2203;
+-----------+-------------------------------------------------------------------+
| Database  | Create Database                                                   |
+-----------+-------------------------------------------------------------------+
| myee_2203 | CREATE DATABASE `myee_2203` /*!40100 DEFAULT CHARACTER SET gbk */ |
+-----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)

-- 删除库
-- drop database 库名 ;
mysql> drop database ee2203;
Query OK, 0 rows affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| myee_2203          |
| mysql              |
| performance_schema |
| test               |
+--------------------+

-- drop database if exists 库名 ; 如果存在这个库删除
mysql> drop database if exists myee_2203;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
Copy after login

Database DDL statement (database definition statement) to create tables, modify tables, query tables, delete tables...

mysql常见的数据类型
     int :整数类型 默认最大长度11位字符,给int类型的数据的时候,当前存储的是值的真实长度
     	举例
     			年龄字段age  int类型
     			
     int(字符数):  整数类型, 	给int(3)这个值的时候,实际存储3位,但是赋值的时候不够3位  (使用很少)
     				id字段 1-----int(3) -------------------001
     				
     varchar(最大支持255个长度): 	字符串类型 
     		指定varchar(指定长度)
     date:仅仅是日期类型
     datetime:日期+时间类型 
     timestap:时间戳 (举例:管理员添加一个商品,商品上架的时间:当前系统瞬时时间 :2021-5-11 16:50分)
     double:小数类型 
     	double(几位数,小数点后保留的位数)
     	
     	举例:
     			double(4,2): 4位,小数点后保留2位
     clob:大字符类型  
     		支持 "大文本"
     blob:大字节类型
     		 最大支持4G
-- 建表之前,必须使用哪个库
-- use 库名;
mysql> use ee_2203;
Database changed
mysql>
/*
  create table 表名(
  		字段名称1 字段类型1,
  		字段名称2 字段类型2,
  		...
  		...
  		字段名称n 字段类型n
  ) ;

*/
mysql> create table student(
    -> id int,
    -> name varchar(10),
    -> age int,
    -> gender varchar(2),
    -> address varchar(50),
    -> socre double(3,1)
    -> );
    Query OK, 0 rows affected (0.02 sec)
    
-- 查询当前库中有哪些表
-- show tables ;
mysql> show tables ;
+-------------------+
| Tables_in_ee_2203 |
+-------------------+
| student           |
+-------------------+
1 row in set (0.00 sec)

mysql>

-- 查看表的结构 
-- desc 表名;
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| name    | varchar(10) | YES  |     | NULL    |       |
| age     | int(11)     | YES  |     | NULL    |       |
| gender  | varchar(2)  | YES  |     | NULL    |       |
| address | varchar(50) | YES  |     | NULL    |       |
| socre   | double(3,1) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)

-- 修改表的字段名称 
-- alter table 表名 change  旧字段名称 新的字段名称 以前的字段数据类型;

mysql> alter table student change gender sex varchar(2) ;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| name    | varchar(10) | YES  |     | NULL    |       |
| age     | int(11)     | YES  |     | NULL    |       |
| sex     | varchar(2)  | YES  |     | NULL    |       |
| address | varchar(50) | YES  |     | NULL    |       |
| socre   | double(3,1) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)

-- 修改表的字段类型 modify
-- alter table 表名 modify 字段名称 新的字段类型 ;
mysql> alter table student modify address varchar(100) ;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | int(11)      | YES  |     | NULL    |       |
| name    | varchar(10)  | YES  |     | NULL    |       |
| age     | int(11)      | YES  |     | NULL    |       |
| sex     | varchar(2)   | YES  |     | NULL    |       |
| address | varchar(100) | YES  |     | NULL    |       |
| socre   | double(3,1)  | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
6 rows in set (0.01 sec)

-- 修改表:给表中添加一个新的字段
-- alter table 表名 add 字段名称 字段类型;
mysql> alter table student add description varchar(200) ;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

-- 复制一张表
-- create table  新表名  like 旧表名;
mysql> create table teachear like student ;
Query OK, 0 rows affected (0.02 sec)

mysql> show tables;
+-------------------+
| Tables_in_ee_2203 |
+-------------------+
| student           |
| teachear          |
+-------------------+
2 rows in set (0.00 sec)

-- 删除表
-- drop table 表名;
-- drop table if exists 表名;
mysql> drop table if exists teachear;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+-------------------+
| Tables_in_ee_2203 |
+-------------------+
| student           |
+-------------------+
1 row in set (0.00 sec)
Copy after login

DML data: database operation statements: records of operation tables

Query data

-- 查询这个库中有哪些表
SHOW TABLES ;

-- 创建一张新的表学生表,id,姓名,年龄,性别,住址
CREATE TABLE student(
    id INT ,  -- 学号
    NAME VARCHAR(10), -- 姓名
    age INT,  -- 年龄
    gender VARCHAR(5), -- 性别
    address VARCHAR(50) -- 住址
) ;

-- 查询学生的表的结构
DESC student ;
Copy after login

Insert data

-- 给学生表中插入数据 
--  插入数据的语法1:insert into 表名 values(值1,值2,值3,值4...值n); 插入全部数据
INSERT INTO student VALUES(1,&#39;高圆圆&#39;,42,&#39;女&#39;,&#39;西安市&#39;) ;

-- 插入表的数据支持 :一次插入多条数据
-- insert into 表名 values(值1,值2,值3,值4...值n),(值1,值2,值3,值4...值n),(值1,值2,值3,值4...值n);
INSERT INTO student VALUES(2,&#39;文章&#39;,35,&#39;男&#39;,&#39;咸阳市&#39;),
(3,&#39;马伊琍&#39;,40,&#39;女&#39;,&#39;上海市&#39;),(4,&#39;马保国&#39;,56,&#39;男&#39;,&#39;宝鸡市&#39;) ;

-- 语法2:插入部分字段,没有插入的字段,默认值就是null, 也支持一次性插入多条数据
-- insert into 表名(字段名称1,字段名称2....) values(值1,值2...) ;
INSERT INTO student(id,NAME,age,gender) VALUES(5,&#39;王宝强&#39;,35,&#39;男&#39;) ;
INSERT INTO student(id,NAME,age,gender) VALUES(6,&#39;张三丰&#39;,60,&#39;男&#39;),(7,&#39;令狐冲&#39;,38,&#39;男&#39;) ;

/*
注意事项:
	1)插入的这些值必须要和表中的字段对应上; 先后顺序保证一致!
	2)目前没有加入"数据库约束",可以插入非法数据,举例:id重复 ,
	后期需要使用数据库约束来限定用户的操作表的行为!
*/
-- 插入一个学生id为7的
INSERT INTO student VALUES(7,&#39;张佳宁&#39;,32,&#39;女&#39;,&#39;西安市&#39;) ;
Copy after login

Modify data

--  一般实际开发中修改:都是带条件修改  (推荐)
-- update 表名 set 字段名称 = 值 where 条件 ;

-- 需求:将name为张佳宁的学生id改为8
UPDATE student SET id = 8 WHERE NAME = &#39;张佳宁&#39; ;

-- 需求: 修改id为6的学生的姓名为 &#39;姚笛&#39; (id字段在实际开发中:都是非业务字段,以后唯一的)
UPDATE student SET NAME = &#39;姚笛&#39; WHERE id =  6 ;


-- 语法2:一次性修改多个字段
-- update 表名 set 字段名称1 = 值 ,字段名称2 =值2.... where 条件;

-- 需求:将id为6的学生 的年龄改为25,性别改为女,地址改为上海
UPDATE student SET age = 25,gender =&#39;女&#39;,address=&#39;上海市&#39; WHERE id = 6 ;


-- 语法3:不带条件 属于批量修改 (部分场景可以用的)
-- update 表名 set 字段名称= 值,字段名称2 =值2....
UPDATE student SET address = &#39;鄠邑区&#39; ;
Copy after login

Delete data

-- delete  from 表名 where 条件 ; 带条件删除记录 (使用的非业务字段id删除)
-- 需求:删除id为7的学生信息
DELETE FROM student WHERE id = 7 ;

-- delete from 表名 :删除全表数据
DELETE FROM student ;


-- truncate table 表名; 删除全表数据
TRUNCATE TABLE student;

/*
   面试题:
      delete from 表名 和 truncate table 表名 :两个区别?
      共同点:都是可以删除全表的记录的;
      不同点:
		delete from 表名 ; 仅仅只是将表的全部记录删除了,表还在!
它针对id(非业务字段:设置主键并且自增长),它不影响自增长主键的这个值; (数据库约束后面讲)
		truncat table 表名; 将表所有数据删除,而且还会把删除之后,
		自动创建一个张一模一样的表,影响自增主键的值!
*/

DROP TABLE student ;
SHOW TABLES ;
-- 创建一个学生表,id 加入主键(非空且唯一)和自增长(不断的自增1)约束
CREATE TABLE student(
	id INT PRIMARY KEY AUTO_INCREMENT , -- id主键并且自增长
	NAME VARCHAR(20),-- 姓名
	age INT 
);
-- 一次插入4条
INSERT INTO student(NAME,age) VALUES(&#39;高圆圆&#39;,42),(&#39;张佳宁&#39;,32),(&#39;文章&#39;,35),(&#39;王宝强&#39;,38) ;

-- 自增长的id可以自己给个值
INSERT INTO student  VALUES(15,&#39;高圆圆2&#39;,42);
INSERT INTO student(NAME,age) VALUES(&#39;姚笛&#39;,35) ;
Copy after login

DQL statement: (database query statement)

-- DQL语句最通用的语法:查询表的全部数据
-- -- * 代表所有字段,仅仅是自己玩的时候可以用,实际开发中不能用*,需要写上全部的字段名称
SELECT * FROM student ; 




CREATE TABLE student3 ( 
	 id INT, -- 编号
	 NAME VARCHAR(20), -- 姓名
	 age INT, -- 年龄 
	 sex VARCHAR(5), -- 性别 
	 address VARCHAR(100), -- 地址 
	 math INT, -- 数学
	 english INT -- 英语
  );
 INSERT INTO student3(id,NAME,age,sex,address,math,english) 
 VALUES (1,&#39;马云&#39;,55,&#39;男&#39;,&#39; 杭州&#39;,66,78),
 (2,&#39;马化腾&#39;,45,&#39;女&#39;,&#39;深圳&#39;,98,87),
 (3,&#39;马景涛&#39;,55,&#39;男&#39;,&#39;香港&#39;,56,77),
 (4,&#39;柳岩 &#39;,20,&#39;女&#39;,&#39;湖南&#39;,76,65),
 (5,&#39;柳青&#39;,20,&#39;男&#39;,&#39;湖南&#39;,86,NULL),
 (6,&#39;刘德华&#39;,57,&#39;男&#39;,&#39;香港 &#39;,99,99),
(7,&#39;马德&#39;,22,&#39;女&#39;,&#39;香港&#39;,99,99),
(8,&#39;德玛西亚&#39;,18,&#39;男&#39;,&#39;南京&#39;,56,65);
Copy after login

1) The most basic query statement select

-- 查询全表数据:select * from 表名;
SELECT * FROM student3 ;
-- 实际开发中查询全部字段,把字段名称全部写上
SELECT 
	id,
	NAME,
	age,
	sex,
	address,
	math,
	english

FROM 
	student3;
	
-- 查询全部字段的时候,给字段起一个别名  as &#39;别名名称&#39; ,as省略
SELECT 
  id AS &#39;编号&#39;,
  NAME AS &#39;姓名&#39;,
  age AS &#39;年龄&#39;,
  sex AS &#39;性别&#39;,
  address AS &#39;地址&#39;,
  math AS &#39;数学成绩&#39;,
  english AS &#39;英语成绩&#39; 
FROM
  student3 ;
-- as 可以省略的

SELECT 
  id  &#39;编号&#39;,
  NAME  &#39;姓名&#39;,
  age  &#39;年龄&#39;,
  sex  &#39;性别&#39;,
  address  &#39;地址&#39;,
  math  &#39;数学成绩&#39;,
  english  &#39;英语成绩&#39; 
FROM
  student3 ;
 -- 当表的名称比较长的时候,可以给表名起一个别名;
SELECT 
  s.`id` &#39;学生编号&#39;,
  s.`name` &#39;学生姓名&#39;,
  s.`age` &#39;学生年龄&#39;,
  s.`sex` &#39;学生性别&#39;,
  s.`address` &#39;学生地址&#39;,
  s.`math` &#39;数学成绩&#39;,
  s.`english` &#39;英语成绩&#39; 
FROM
  student3 s ;-- 起了一个别名s 
    
  -- 可以查询部分字段
 -- 需求:查询学生的姓名以及数学和英语成绩
 SELECT 
	NAME &#39;姓名&#39;,
	math &#39;数学成绩&#39;,
	english &#39;英语成绩&#39;
 FROM
     student3 ;
-- 需求:查询学生的地址信息
SELECT 

	address

FROM 
	student3;
-- 发现:字段冗余(重复度大) 字段去重      DISTINCT 后面跟上字段名称  
SELECT  DISTINCT address FROM student3;
Copy after login

2) DQL statement’s conditional query where keyword

 -- 2.1)使用 赋值运算符=,比较运算符 <,<=,>=,>,!=,   mysql中的不等于 <>
 -- 2.2)Java中逻辑运算符:&&,||  mysql推荐使用 and , or
 -- 2.2)针对两个范围查询: 可以使用&&,可以使用and, 也可以 "字段名称 between 值1 and 值2"
 -- 需求:查询年龄大于20岁的学生所有信息     
SELECT
	 *
FROM
	student3 
	
WHERE  
	age > 20 ;

-- 需求:查询年龄在20到30之间的学生的姓名,年龄,住址 ,数学和英语成绩信息

SELECT 
  NAME &#39;姓名&#39;,
  age &#39;年龄&#39;,
  address &#39;住址&#39;,
  math &#39;数学成绩&#39;,
  english &#39;英语成绩&#39; 
FROM
  student3 
WHERE age >= 20 && age <= 30 ; -- && 逻辑双与

-- mysql中使用and 并列关系
SELECT 
  NAME &#39;姓名&#39;,
  age &#39;年龄&#39;,
  address &#39;住址&#39;,
  math &#39;数学成绩&#39;,
  english &#39;英语成绩&#39; 
FROM
  student3 
WHERE age >= 20 AND age <= 30 ; -- and 连接


-- 使用的between 值1 and 值2
SELECT
    NAME &#39;姓名&#39;,
    age &#39;年龄&#39;,
    address &#39;住址&#39;,
    math &#39;数学成绩&#39;,
    english &#39;英语成绩&#39; 
FROM
    student3 
WHERE 
	age BETWEEN 20  AND 30 ;

-- 需求:查询年龄是20岁学生的所有信息
SELECT  * FROM student3 WHERE age = 20 ;

-- 需求:查询年龄不是20岁的学生的所有信息
SELECT * FROM student3 WHERE age != 20 ; -- != Java中 的用法

SELECT * FROM student3 WHERE age <> 20 ;-- mysql中的不等于 <>


-- 需求:查询年龄是18岁或者是20或者是45岁的学生的所有信息
SELECT 
	* 
FROM 
	student3
WHERE 
	age = 18 || age =20 || age = 45 ;-- Java中逻辑双或||
	
	
SELECT 
	* 
FROM 
	student3
WHERE 
	age = 18 OR age =20 OR age = 45 ; -- Mysql中的or这个表示 或(并集)

-- 上面这个格式优化为 in(值1,值2,值3..值n) ;in集合语句	
	
SELECT  
    *
FROM
    student3
WHERE 
	age IN(18,20,45) ;
	
	
-- Java语言中:去判断某个条件的内容为null ,mysql语言不支持这个格式 ==null
-- 需求:查询学生的英语成绩为null的学生所有信息
-- select * from student3 where english == null ;
-- mysql中判断某个字段为null,使用的语法是 is null 
-- 判断某个字段不为null,使用的语法是 is not null

SELECT * FROM student3 WHERE english IS NULL ;
-- 需求:查询学生的英语成绩不为null的学生的所有信息
SELECT * FROM student3 WHERE english IS NOT NULL ;	



-- 关于int类型字段求和的时候,注意: int类型的值 + null 值 = null;

-- 查询学生的姓名以及英语和数学总分
SELECT  
    NAME &#39;姓名&#39;,
    (math+english) &#39;总分&#39;
FROM
    student3 ;
-- 上面这种情况:不太友好,数学成绩有值,但是结果求和是null
-- mysql提供函数 ifnull(字段名称,值);  如果字段名称是null,给一个默认值
SELECT  
    NAME &#39;姓名&#39;,
    (math+IFNULL(english,0)) &#39;总分&#39;
FROM
    student3 ;
    
SHOW VARIABLES LIKE &#39;%character%&#39; ;
Copy after login

3) DQL statement’s where condition is added after Fuzzy query—keyword like

-- select 字段列表 from 表名 where 字段名称 like &#39;%xxx%&#39; ;
-- %:代表任何多个字符或者某个字符   (使用居多):网站门户系统---"搜索商品" ,模糊搜索
-- _:代表某个单个字符

-- 需求:查询所有姓马的学生信息

SELECT
    *
FROM
    student3
WHERE 
    NAME LIKE   &#39;%马%&#39;
    
-- 查询学生姓名三个字符并且第二个字符是化的人
SELECT 
    *
FROM 	
     student3
WHERE 
     NAME  LIKE &#39;_化_&#39; ;
     
-- 查询姓名为三个字符的学生信息
SELECT 
    *
FROM 
	student3
WHERE 
	NAME LIKE &#39;___&#39; ;
Copy after login

4) Aggregation function query

针对int类型:
		单行单列数据
-- select  聚合函数 from 表名 where 还可以指定条件;

-- count(字段名称):查询表中记录 ,字段名称使用都是id,非业务字段
-- avg(字段名称): 查询这个字段中平均值 
-- sum(字段列表):求和函数
-- max(字段名称):最大值
-- min(字段名称):最小值

-- 需求:查询表的总记录数
-- select count(english) &#39;总记录数&#39; from student3; -- 前提条件使用业务字段查询,不能有null值
-- select count(ifnull(english,0)) &#39;总记录数&#39; from student3; 
SELECT COUNT(id) &#39;总条数&#39; FROM student3 ;
-- 查询数学平均分 avg(字段名称)
SELECT AVG(math) &#39;数学平均分&#39; FROM student3;
-- 数学和英语成绩求和的学生信息(姓名,和总分)
SELECT  
	SUM(math+IFNULL(english,0)) &#39;总分&#39;
FROM
        student3;
-- max()和min()
SELECT MAX(math) &#39;数学最高分&#39; FROM student3 ;


-- select语句嵌套select语句 --- 子查询
-- 需求:查询出学生数学成绩大于 数学平均分的学生所有信息;
-- 使用where 条件 后面带上 比较运算符...

--  Java中
-- int x =10,y ;
-- y = y+x ; 将10代入

-- 1)查询数学平均分是多少
SELECT AVG(math) FROM student3; -- 79.5000
-- 2)查询大于数学平均分的学生信息
SELECT 
	*
FROM 
	student3
WHERE 
	math > 79.5000;
	
-- 一步走
SELECT 
	*
FROM 
	student3
WHERE 
	math >  (SELECT AVG(math) FROM student3) ;
	
-- 查询数学成绩最高分的学生的姓名,年龄,地址以及数学成绩;
-- 1)查询最高分是多少
SELECT  MAX(math) FROM student3; -- 99
-- 2)查询最高分是这个成绩的学生信息
SELECT 
    NAME &#39;姓名&#39;,
    age &#39;年龄&#39;,
    address &#39;地址&#39;,
    math &#39;数学成绩&#39;
FROM 
    student3 
 WHERE 
	math = 99 ;


-- 一步走
SELECT 
    NAME &#39;姓名&#39;,
    age &#39;年龄&#39;,
    address &#39;地址&#39;,
    math &#39;数学成绩&#39;
FROM 
    student3 
 WHERE 
	math = 
	(SELECT  MAX(math) FROM student3) ;
Copy after login

5)DQL sorting query order by

 select 字段列表 from 表名 order by 字段名称 排序规则(asc(默认值就是升序)或者desc 降序)

UPDATE student3 SET english = 88  WHERE id = 5 ;
UPDATE student3 SET english = 94 WHERE id = 6 ;

-- 需求:按照数学成绩升序排序 单个字段
SELECT 
   *
FROM 
    student3
ORDER BY 
	math ASC ; -- 默认不写排序规则,就是asc 升序

-- 同时条件和order by,order by在where的后面
-- 需求:数学成绩大于56分的学生进行数学的降序排序
SELECT
   NAME ,
   math
FROM
    student3 
WHERE  math > 56    -- 先满足条件,才能排序!where 必须order by之前
ORDER BY math DESC ;

-- 多个字段要同时排序,首先第一个字段排序规则,然后才是第二个字段
-- 需求:数学成绩降序,英语成绩升序排序
SELECT 
    *
 FROM 
    student3 
 ORDER BY 
	math DESC , english ASC ;

SELECT * FROM student3 ;
Copy after login

6)DQL statement group query: group by

-- select 字段列表 from 表名  group by 分组字段名称;

-- 注意事项:  1)查询的字段列表中可以使用 分组字段
     --       2)group by之后不能使用聚合函数

-- 需求:按照性别分组,查询出他们的数学成绩的平均分
SELECT  
     sex &#39;性别&#39;, -- 查询的分组字段
     AVG(math) &#39;数学平均分&#39; -- 查询每一个组的数学平均分
FROM
	student3 
GROUP BY 
	sex ;
	
-- 带条件分组查询的语法:  where 条件 必须放在group by 之前,否则语法错误!
-- select 字段列表包含分组字段,聚合函数.. from 表名 where 条件  group by 分组字段;

-- 需求:按照性别分组,查询出他们的数学成绩的平均分,数学成绩大于70分的参与分组

 SELECT
     sex &#39;性别&#39;,
     AVG(math) &#39;数学平局分&#39;
 FROM

	student3

WHERE 
	math > 70    -- 先满足条件,然后才能分组;

GROUP BY 	
	sex   ;
Copy after login

7) Filter query having

-- where条件,group by,having  必须先有条件,分组,然后才筛选!
-- 注意:筛选的后面可以使用聚合函数,group by的后面是不能使用聚合函数的
-- 需求:按照性别分组,查询数学平局分,条件:数学成绩大于70的人参与分组, 筛选出人数大于2的这一组
SELECT 
     sex  &#39;性别&#39;,
     COUNT(id) &#39;总人数&#39;,
     AVG(math) &#39;数学平均分&#39;
     
FROM
    student3	
WHERE 
	math > 70
GROUP BY 
	sex 
HAVING      -- 后面可以使用聚合函数
	COUNT(id) > 2 ;
	
-- 优化为
SELECT 
     sex  &#39;性别&#39;,
     COUNT(id) 人数,
     AVG(math) &#39;数学平均分&#39;
     
FROM
    student3	
WHERE 
	math > 70
GROUP BY 
	sex 
HAVING      -- 后面可以使用聚合函数
	人数 > 2 ;
Copy after login

8) Paging query limit

-- select 字段列表 from 表名   limit 起始行数,每页显示的条数;
	
-- 起始行数:从0开始算的,    
-- 起始行数 = (当前页码-1)*每页显示的条数
-- 前提条件:每页显示2条, 
-- 第一页数据
SELECT * FROM student3   LIMIT 0,2;

-- 第二页数据
SELECT * FROM student3 LIMIT 2,2 ;

-- 第三页数据
SELECT * FROM student3 LIMIT 4,2 ;

-- 第四页数据
SELECT * FROM student3 LIMIT 6,2 ;

-- 第五页数据
SELECT * FROM student3 LIMIT 8,2;

-- 第六页数据
SELECT * FROM student3 LIMIT 10,2;

-- 第七页数据

SELECT * FROM student3 LIMIT 12,2;
Copy after login

[Related recommendations: javascript video tutorial, programming video

The above is the detailed content of Is javascript a sql language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template