«

mysql 查询

时间:2023-3-1 22:11     作者:wen     分类: MySQL


show variables like '%character%';

 character_set_client     | utf8 客户端字符集
 character_set_connection | utf8 链接数据库用的字符集
 character_set_database   | utf8 数据库中的字符集
 character_set_filesystem | binary
 character_set_results    | utf8 返回结果集的字符
 character_set_server     | utf8 服务器字符集
 character_set_system     | utf8

my.ini
character-set-server = utf8
default-character-set = utf8

增
insert into 表名(字段) values(值);
删
delete from 表名 where 条件
改
update 表名 set 字段=value where 条件
查
select * from 表名
select 字段,字段... from 表名

select 字段列表 from 表名 [where 条件][order by 字段 asc/desc][limit 起始位置,长度][group by 字段名[having ...]]

插入
alter table 表名 modify 字段 类型 修改语句

练习:
select * from books order by price desc where price between 42 and 63 limit 0,3;

[]需要的时候加

字段别名 as
 select 表名 as 别名,表名 as 别名...;

order by 字段名 asc|升序 desc|降许
 select * from 表名 order by 字段名 asc|升序 desc|降许;

mysql 命令不区分大小写

limit 起始位置,长度 (其实位置0开始计数)
选择|截取部分记录
 select * from 表名 limit 起始位置,长度;

where 条件
 字段 between num and num 之间
选择|截取连续空间
 select * from 表名 where 字段 between num and num;
选择|截取不连续的空间
 select * from 表名 where 字段 between in(num,num,...);

 select * from 表名 where 字段 between[>|<|=|>=|<=|<>]num;
逻辑运算符
 and or not | 与 或 非
select * from 表名 where 字段 between=num or between=num;
select * from 表名 where 字段 between>num and between<num;
select * from 表名 where 字段 not between>num and between<num;

模糊搜索 where 字段名 like "%字符%"
select * from 表名 where 字段名 like "%字符%";
   like 效率不高
   % 任意多个字符
   _ 任意一个字符

子查询:
   查找的是谁
   select * from 表名1 where 字段名=(select 字段名 from 表名2 where 字段名='字符');
连接查询
   内连接
      select * from 表1,表2 where 共有字段=共有字段|student.sid = grade.sid;
   外连接
    左连接
     select 字段列表 from 主表 left join 子表 on 链接条件 [where 其它条件]
    右连接
    select 字段列表 from 子表 right join 主表 on 链接条件 [where 其它条件]
*********************************************************************************************************
   select books.bid,books.bname,books.btypeid,books.publishing,books.price,books.pubdate,books.author,books.lsbn,btype.btypename from books left join btype on books.btypeid = btype.btypeid;
*********************************************************************************************************
需求分析
   分析数据
      1.导航数据(频道)
        概要设计
        详细设计
        代码
        测试
        维护
...