zoukankan      html  css  js  c++  java
  • mysql,增删改,多表查询--L

    1. 简单增删改
      -- 创建一个member表
      -- id、user_name,phone,pwd  -- sql 大小写不区分--》弱语法
      create table member(
          id int(11),
          user_name varchar(20),
          phone char(11),
          pwd varchar(40)
      );

      -- 1:增--》保存数据的--》增加数据
      -- 语法 :insert into 表名 values(每个字段的值);  -- 背
      -- 数值不用加,字符串的话要加''
      -- 弱语法
      -- MD5:内置的函数
      insert into member values(3,'happy',13777777777,MD5('123456'));
      -- 只插入一部分列的数据
      -- 语法 :insert into 表名(要插入列名) values(每个字段的值); 
      insert into member(id,user_name,phone) values(3,'happy','13777777777');
      -- 一条语句插入多条记录
      -- 语法 :insert into 表名(要插入列名) values(每个字段的值),(每个字段的值),(每个字段的值); 
      insert into member(id,user_name,phone) 
      values(5,'tom1','13777777777'),
      (6,'tom2','13777777777'),
      (7,'tom3','13777777777'),
      (8,'tom4','13777777777'),
      (9,'tom5','13777777777');

      -- 2:删除 --关键字
      -- 1)删除表的所有记录
      -- 语法:delete from 表名;
      delete from member;
      -- 2)删除部分记录
      -- 我要把tom1的用户删掉--》我要删记录,从user_name=tom1这个条件下删除
      -- 语法:delete from 表名 where 指定的条件;
      delete from member where user_name='tom1';
      -- 3)删除整个表的记录(删除所有记录,而且会把自增长恢复到默认值)
      truncate table member;
      -- 4)删除表(破坏性,谨慎操作,删除结构)
      drop table member;

      -- 3:改--》update -->更新
      -- 1):修改所有记录
      -- 语法:update member set 字段1='新的值',字段2='新值'...
      -- 更新会员表的密码,设置这个密码等于MD5('lemon123456')
      update member set pwd= MD5('lemon123456');
      -- 2):修改部分记录
      -- 语法:语法:update member set 字段1='新的值',字段2='新值' where 条件;
      update member set pwd= 'abcdef',phone='13555555555' where user_name='tom1';

      -- 4:查询--select 
      -- 1)查询表的所有记录
      -- 语法:select 检索的字段 from 表名;
      select id,phone from member;
      
      -- 2)查询表的部分记录
      -- 筛选,过滤,部分-->where
      -- 语法:select 检索的字段 from 表名 where 指定的条件;
      select id,phone from member where user_name='tom1';
      
      -- 如果一个表有20个字段
      -- *号表示所有的字段
      select * from member;
      
      EXPLAIN EXTENDED
      select * from member;
      SHOW WARNINGS;
    2.  多表查询
    3.  多表查询实例
      
      
      -- 单表查询
      select * from t_user; -- 用户信息表
      select * from t_lover_info;-- 用户的对象信息表

      -- 多表查询
      -- 1:笛卡尔积:
      select * from t_user,t_lover_info; -- 45条 = 5*9
      -- 2:都是正确的吗??有意义的吗?
      -- 筛选出有意义的记录
      -- 查询出有对象的用户信息
      -- 等值连接(通过条件过滤出有意义的记录)
      select * from t_user as t1,t_lover_info as t2 where t1.id = t2.u_id; -- 条件
      select * from t_user as t1 join t_lover_info as t2 on t1.id=t2.u_id;
      select * from t_user as t1 inner join t_lover_info as t2 on t1.id=t2.u_id;-- 内连接
      select * from t_user as t1 cross join t_lover_info as t2 on t1.id=t2.u_id; 
      
      -- 3:左链接
          -- 1)查询出所有的用户信息及其对象信息
      -- 左连接
      select * from t_user as t1 left join t_lover_info as t2 on t1.id=t2.u_id;-- 内连接
      
          -- 2)查询出没有对象的用户信息
      select * 
      from t_user as t1 left join t_lover_info as t2 
      on t1.id=t2.u_id
      where t2.u_id is null; -- 我的对象为null
      
      --  3)查询有对象的用户信息-- 有对象
      select * 
      from t_user as t1 left join t_lover_info as t2 
      on t1.id=t2.u_id
      where t2.lover_name is not null;
      
      -- 右连接--》左连接
      select * from t_lover_info as t2 right join t_user as t1 on t1.id=t2.u_id;-- 内连接
      
      
      EXPLAIN EXTENDED
      select * from t_user as t1,t_lover_info as t2 where t1.id = t2.u_id; 
      show WARNINGS;
    4.  
    5.  
    ------------------------这是用来做笔记的,可能不够详细,如有问题可以留言-------------------------
  • 相关阅读:
    CCCC L3-015. 球队“食物链”(dfs+剪枝)
    【USACO2.1】解题报告
    【USACO2.1】解题报告
    序列【模拟】
    序列【模拟】
    【JZOJ5184】Gift【DP】【01背包】
    【JZOJ5184】Gift【DP】【01背包】
    【JZOJ5177】TRAVEL【并查集】
    【JZOJ5177】TRAVEL【并查集】
    【JZOJ5178】So many prefix?【KMP】【DP】
  • 原文地址:https://www.cnblogs.com/focusta/p/12217044.html
Copyright © 2011-2022 走看看