暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

数据库的基本操作2

老徐今天也很棒 2021-06-28
310

按条件查询:

select username,password from users where id=1;

in 查询

select * from users where id in (1,2,3);

select * from users where id not in (1,2,3);

between and 查询

select * from users where id between 1 and 10;

select * from users where id not between 1 and 10;

like 查询

select * from users where username like "%m%"

and 查询

select * from users where id=1 and username='xxx';

or 查询

select * from users where id=1 or username='xxx';

当 and 与 or  一起使用时:and的优先级高于or,先运算and再运算or。

聚合查询

count() 返回行数;

select count(*) from users;

select count(id) from users;

分组查询

GROUP BY

mysql> SELECT * FROM users GROUP BY password;

+----+----------+----------+

| id | username | password |

+----+----------+----------+

|  3 | moon1    | 123456   |

|  1 | moon     | 456789   |

+----+----------+----------+

2 rows in set (0.01 sec)

mysql> SELECT * FROM users GROUP BY username;

+----+----------+----------+

| id | username | password |

+----+----------+----------+

|  2 | alex1    | 456789   |

|  1 | moon     | 456789   |

|  3 | moon1    | 123456   |

+----+----------+----------+

3 rows in set (0.01 sec)

limit 查询

select * from users limit 2,5;

子查询

  • where型子查询   把内层查询结果当作外层查询的比较条件

  • from型子查询   把内层查询结果当作外层查询的比较条件

  • exists型子查询  把外层查询结果拿到内层,看内层的查询是否成立

  • union 联合查询 两次查询的列数必须一致

内连接

inner join on:结合两个表中的记录,返回关联字段相符的记录,也就是两个表的交集部分。

select * from a_user as a inner join b_class on a.class_id=b.user_id;

左链接

left join on:左表的记录都会显示出来,而右表只会显示符合搜索条件的记录。右表记录不足的地方均为NULL。

select * from a_user as a left join b_class as b on a.class_id=b.user_id;

右链接

right join on:右表的记录都会显示出来,而左表只会显示符合搜索条件的记录。左表记录不足的地方均为NULL。

select * from a_user as a right join b_class as  b on a.class_id=b.user_id;

跨库查询:information_schema

schemata表:提供了当前mysql中所有数据库的信息

tables表:提供了关于数据库中的表的信息

columns表:提供了表中的列信息

and 1=2 union select 1,2,schema_name from information_schema.schemata limit 0,1

select 1,2,table_name from information_schema.tables where table_schema='' limit 0,1

select 1,2,column_name from information_schema.columns where table_name='' limit 0,1




文章转载自老徐今天也很棒,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论