MySQL学习笔记 SQL学习

1. sql语句新增字段:

ALTER table TABLE_NAME(表名) ADD CULUMN `field(字段)` 数据类型 是否无符号 是否为空 是否设置默认值;

2. 删除字段sql语句:

 ALTER TABLE TABLE_NAME DROP COLUMN COLUMN_NAME

3. 修改字段sql语句

alter table TABLE_NAME  modify column COLUMN_NAME varchar(20) COMMENT '注释';

4. 使用DISTINCT规避重复的数据

SELECT
DISTINCT
	c.`name`,
	cc.`name` AS class_name,
	c.`id`,
	c.`cid` 
FROM
	sycms_question AS q
	RIGHT JOIN course AS c ON q.cid = c.id
	LEFT JOIN course_class AS cc ON c.cid = cc.id 
WHERE
	`cc`.`id` = 63 
ORDER BY
	c.id DESC 
GROUP BY
	c.`name`
LIMIT 0,5

5. 或者使用group by进行去重

6. 获取一个表内2个字段相同的数据

SELECT
	`id`,
	`status`,
	`quantity`,
	`take_count` 
FROM
	coupon AS a 
WHERE
	EXISTS ( SELECT quantity, take_count FROM coupon WHERE quantity = a.take_count GROUP BY quantity HAVING count( * ) > 1 )

7. 开放数据库访问权限

开放远程连接权限:grant [权限] on   [database.table] to [用户名]@[IP] identified by [密码];  

刷新权限:flush privileges;

例子:

grant all privileges on test.* to user@'%' identified by '1234';  

意思是,test数据库的所有表对任意IP地址的user用户开放所有权限,登陆密码是1234。

8. 当MySQL的自增id用完了怎么办?

MySQL中,Int整型的范围如下:

类型 最小值 最大值 存储大小
Int 有符号 -2147483648 2148483648 4 bytes
Int 无符号 0 4294967295 4 bytes

以无符号整型为例,约43亿,一旦达到最大值,此时数据继续插入会报一个逐渐冲突异常如下所示

Duplicate entry '4294967295' for key 'PRIMARY'

解决方案就是把Int类型改为BigInt类型,它的范围如下

类型 最小值 最大值 存储大小
BigInt 有符号 -9223372036854775808 923372036854775808 8 bytes
BigInt 无符号 0 18446744073709551615 8 bytes

更好的回答是

进行分库分表

  1. 线上如何修改列的数据类型

    ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
    

    不保险还是用第三方工具进行修改表结构

9.字母加数字拼接的内容查询

比如查询 CS001~CS020之间的所有卡号

select * from coupon 
where number like 'CS%'
and CONVERT(REPLACE(number, "CS",""),SIGNED) >= 1
and CONVERT(REPLACE(number, "CS", ""),SIGNED) <= 20

select * from table where CONVERT(replace(起始卡号, "固定前缀", ""),SIGNED) < 查询的号码

Mysql数据库中查询某表中第二大的数据

leetcode记录

编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+
# 1. 查询出最大的记录,然后查询剩下记录中比该记录小的最大数据记录 
select max(Salary) as SecondHighestSalary
from Employee where Salary < (select max(Salary) from Employee);

# 2. 先取得最大的,然后not in 最大的那个,在剩下的取最大的就是第二个。
select max(Salary) from Employee where Salary not in (select max(Salary) from Employee);

# 3. 使用limit offset
select Salary from Employee order by Salary limit 1,1;
select Salary from Employee order by limit 1 offset 1

MySQL查找重复的电子邮箱

重复的电子邮箱存在多次。要计算每封电子邮件的存在次数,我们可以使用以下代码

select Email, count(Email) as num
from Person
group by Email;
| Email   | num |
|---------|-----|
| a@b.com | 2   |
| c@d.com | 1   |

GROUP BY 添加条件的一种更常用的方法是使用 HAVING 子句,该子句更为简单高效。

select Email
from Person
group by Email
having count(Email) > 1;

知道使用group by和having。还需要记得优先顺序。where>group by>having>order by

相比于执行速度来说,下面的更快

select distinct a.Email from Person a, Person b where a.Email=b.Email and a.Id <> b.Id;