首页 > 技术文章 > SQL

pengpp 2017-12-01 15:18 原文

一、 SQL常用功能

功能 关键字 语法
创建/删除数据库 create/drop create DATABASE 数据库名;drop database 数据库名
创建/删除 create/drop create table 表名;drop table 表名
查询 seclect(like,in,or,between...and)

seclect * (列名1,列名2, 列名3) from 表名 where 条件;

模糊查找:select * from table where 列名 like ’%value1%’ (所有包含‘value1’这个模式的字符串);

指定查询:select * from table where 列名 in (23,78,89);

区间查询:select * from table where 列名 between 23 and 89;

插入 insert
insert into table values(value1,value2);
删除 delete delete from table where 条件
更新 update update table set 列名=value where 条件
排序 order by select * from table order by 列名1,列名2 [desc];ASC为升序 默认不写; DESC为降序
计数 count(*) select count(*) as 总数 from table;
去重 distinct select distinct 列名 from 表名 ;
分组 group by seclect * (列名1,列名2, 列名3) from 表名where 列名 like 'values%' group by 列名 having 条件
求和 sum select sum(列名) as 总和 from table
平均 avg select avg(列名) as 均值 from table
最大 max select max(列名) as 最大值 from table
最小 min select min(列名) as 最小值 from table[separator]

Practise:

1. 有学生信息表,字段有:Ssid(学号),Sname(姓名),Sage(年龄),Ssex(性别),Sadr(地址)。

(1)创建学生表Student,要求学号为主键,性别默认为Y/N或者设为“男”,“女”。

(2)查找姓“张”的所有学生信息。

扩展:

(1)统计男生女生各有多少人?

#创建数据库student
create database student; 
#使用数据库
use student;
#创建学表
create table Student (
Ssid varchar(16) primary key,
Sname varchar(10),
Sage int,
Ssex enum('',‘女’),
Sadr varchar(32));

或者

或者

插入数据:

查找姓张的学生:

 

统计男生女生各多少人:

 2. 另有班级表:

     班级编号     班级名称     班级楼层
        clid      clname       clfloor

学生成绩表:

       学生学号     学生班级     学科   成绩
          Ssid       clid   course   score

(1)求学生“王子”所在的班级名称。

(2)查找数学成绩超过90分的学生人数大于5的班级。

三张表的信息如下:

(1)求学生“王子”所在的班级名称:

select clname from student a, stuscore b, classinfo c where a.Sname ='王子' and a.Ssid=b.Ssid and b.clid=c.clid group by c.clname;

或者:

select clname from classinfo t1 inner join ( select clid from student a, stuscore b where a.Ssid=b.Ssid and a.Sname='王子' group by clid) t2 on t1.clid=t2.clid;

(2)查找数学成绩超过90分的学生人数大于5的班级。

select clname from classinfo t1 inner join (select clid, count(*) as rf from stuscore where course = '数学' and score >90 group by clid having rf
>=2) t2 on t1.clid=t2.clid;

 二、 Redis相关操作

1、redis 缓存冷启动:redis中没有任何的缓存数据,redis cluster 集群就启动的现象成为redis缓存冷启动。 结果是:若不进行数据预热,就大量请求进来,可能会造成Mysql崩溃。

(1)批量导入redis数据:

shell脚本如下:

for((i=1;i<=1000000;i++))
do
    echo "set k$i v$i" >> /tmp/_t.txt
done

使用unix2dos进行格式转换:

unix2dos /tmp/_t.txt

pipeline导入:

cat /tmp/_t.txt | redis-cli -p 6380 -h 192.168.1.166 --pipe

(2)从mysql批量导入数据到redis中:

表 user_gold(user_id, amount) ,每个用户一个key,string类型。将sql 保存到 1.sql中:

SELECT CONCAT(  
  "*3\r\n",  
  '$3\r\n',  
  'SET\r\n',  
  '$', LENGTH(redis_key), '\r\n',  
  redis_key, '\r\n',
  '$', LENGTH(redis_value), '\r\n',
  redis_value, '\r'
)  
FROM (  
  SELECT 
  CONCAT('user:gold:',user_id) AS redis_key,
  amount AS redis_value
  FROM test.user_gold
) AS t;

执行导入操作:

mysql --raw --skip-column-names -h2.6.3.101 -P1067 -uroot -p'123456' test -A < /data/sql/1.sql | /data/redis/bin/redis-cli -a "password" -n 0 -p 6379 --pipe

 参数说明:

--raw:使mysql不转换字段值中的换行符

--skip-column-names:使mysql输出的每行中不包含列名。

推荐阅读