查询语句补充

子查询:

子查询 指的是 当一个查询语句被作为另一个查询语句的条件时,该查询语句就称之为子查询(内层查询) 可以将一个大的问题 拆分几个小的问题 然后一步一步来查询

事例:

create table emp (id int,name char(10),sex char,age int,dept_id int,job char(10),salary double);

insert into emp values

(1,"刘备","男",26,1,"总监",5800),

(2,"张飞","男",24,1,"员工",3000),

(3,"关羽","男",30,1,"员工",4000),

(4,"孙权","男",25,2,"总监",6000),

(5,"周瑜","男",22,2,"员工",5000),

(6,"小乔","女",31,2,"员工",4000),

(7,"曹操","男",19,3,"总监",10000),

(8,"司马懿","男",24,3,"员工",6000);

 

create table dept(id int primary key,name char(10));

insert into dept values(1,"市场"),(2,"行政"),(3,"财务");

 

需求:财务部有哪些人 连接查询: select *from emp join dept on emp.dept_id = dept.id where dept.name = "财务";

子查询的语法: 将子查询(内层查询) 用括号包裹即可 子查询的实现思路 1.通过部门名称拿到部门的id select id from dept where name = "财务"; 2.通过部门id去 员工查询对应的员工 select *from emp where dept_id = (select id from dept where name = "财务");

查询平均年龄大于25的部门名称
    1.先查询出 一堆 平均年龄大于25的部门id
    2.在通过id 查询部门的名称
    select name from dept where id in
    (select dept_id from emp group by dept_id having avg(age) > 25);

查询平均年龄大于25的部门名称
使用连接查询完成
    1.先连接在一起
    2.on 筛选正确匹配关系
    3.where 条件

    select dept.name from emp join dept
    on emp.dept_id = dept.id
    group by dept.name
    having avg(age) > 25;

 

查询语句无论多长 其实都是一步一步做出来的 先写一段测试一下没问题 在接着写

 

 

用户管理;

mysql用户指的是客户端连接服务器时使用的账户 在一些公司中,很多项目的数据 可能会放在同一个服务器, 那就必须要为每一个用户明确其所拥有的的权限 通常 到公司之后 都会给你一个账号名称和密码 并且 为你制定你可以访问那些数据库或表

对用户账号增删改查 以及权限的增删改查

mysql 与权限相关的表:
    user columns_priv  tables_priv db

    select *from user \G;  # 当字段太多 一行 显示不了 可以\G

create user 用户名@主机名称  identified by "123";
该语句只是单纯创建一个用户 后续还要分配权限 稍微麻烦

推荐使用grant 语句 可以在创建账户的同时分配权限
grant all on *.*  to 用户名@主机名 identified by "密码";
grant all on *.*  to jack@localhost identified by "123";
# 授予 所有库的所有表的所有权限 给 jack这个账户 密码为123  只允许在127.0.0.1上登录
  主机地址可以是% 意味着这个用户可以在任何主机上登录服务器  需要加上双引号
# 这个用户不能其他用户授权  默认只有root可以为其他账户授权

 

mysql的权限可以精确到列(某个字段的某种权限)
但是不能精确到某一行(需要使用视图)

删除用户
drop user username@host;

刷新权限
flush privileges

 

pymysql:

import pymysql
try:
    # 1.conn是一个表示连接的对象
    conn = pymysql.connect(
        host="xxx.x.x.x",
        port = 3306,
        user = "root",
        password = "123321",
        database = "testDB")
    print("连接服务器成功!")

    #2. 查询数据 需要借助cursor类  游标   默认游标返回值类型为元组
    # pymysql.cursors.DictCursor  可以将结果转为字典
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    # 3.执行sql语句
    sql = "select *from user;"
    res = cursor.execute(sql) #res为本次查询得到的记录条数
    print(res)


    # 4.提取数据
    print(cursor.fetchall())  # 提取本次查询所有结果
    # print(cursor.fetchone())  # 提取本次查询的第一条记录
    # print(cursor.fetchone())  # 提取本次查询的第一条记录
    # print(cursor.fetchmany(2))  # 指定提取记录的条数

    # 如果游标已经到达最后 将无法在读取到数据 可以使用scroll来移动游标位置
    cursor.scroll(-1,mode="relative")
    # mode参数表示 是相对位置relative 还是绝对位置 absolute
    print(cursor.fetchall())  # 提取本次查询所有结果

except Exception as e:
    print(type(e),e)
finally:
    # 无论是否执行成功 最后都需要关闭连接
    if cursor:cursor.close() # 关闭游标
    if conn:conn.close() # 关闭连接


pymysql 使用
    1.用pymysql.connect(参数)   建立连接  得到连接对象
    2.通过连接对象 拿到游标 对象 conn.cursor(pymysql.cursor.DictCursor)
    3.通过调用游标对象的 excute 或者 excutemany  来执行sql
    4.调用游标的fetch(one/many/all)相关函数 来提取执行结果

    强调: pymysql 默认不会提交修改   需要手动调用conn.commit  或是 在创建时指定自动提交
        scroll 移动游标 不常用

import pymysql

# 1.conn是一个表示连接的对象
conn = pymysql.connect(
    host="xxx.x.x.x",
    port = 3306,
    user = "root",
    password = "123321",
    database = "testDB",
    autocommit = True)
print("连接服务器成功!")

# 增加数据
# sql = "insert into user values(null,'tom','123')"

# 删除
#sql = "delete from user where name = 'tom'"

# 更新
# sql = "update user set id = 10000 where id = 1"

cursor = conn.cursor(pymysql.cursors.DictCursor)
# 返回的是 本次sql语句执行后 受到影响行数
# count = cursor.execute(sql)

# 用来批量添加数据   可提高效率
count = cursor.executemany("insert into user values (null,%s,%s)",[("tom1","123"),("tom2","123"),("tom3","321")])

if count:
    print("修改成功!")
else:
    print("修改失败!")

# 需要调用commit来提交修改  或者在创建连接时  指定自动提交修改
# conn.commit()
 

 

 

 

 

你可能感兴趣的:(查询语句补充)