zoukankan      html  css  js  c++  java
  • mysql

    连接相关操作:

      #mysql -uroot -p

      #show databases;

      #use   XXX_database;

      #show tables;

    建表语句:

    create table 表名(列名称,列类型 [列属性][默认值]),
    engine 引擎名 charset 字符集

    增:
    往那张表增,增哪几列,各为什么值;
    insert into 表名 (列1,列2, ... 列n)
    values
    (值1, 值2.......值N)

    如果不声明拆入的列,则默认拆入所有列;

    改:update
    修改哪张表,修改哪几列,修改成什么值?
    在哪几行上生效

    update 表名
    set
    列1 = 值1
    列2 = 值2
    .。。
    列N = 值N

    where 表达式


    delete:
    删除哪几张表的数据,删那些行

    delete from 表名
    where 表达式

    查:
    select * from 表名

    查询5种子句:where 后面的表示式子代入到每行,确认是否成立;
    where shop_price - market_price > 200;

    in(值1, 值2, 值3, ...值N)等于值1-N任意之一,都可以;
    select good_id, cat_id from goods where cat_id in (4, 5);

    between 在某一范围内;
    between 值1 and 值2, 表示在值1和值2之间(允许等于边界);
    select good_it, cat_id from goods where cat_id between 1 and 6;

    or用法
    select good_id, good_name, shop_price from goods where shop_price >=3000
    and shop_price <=5000 or shop_price >=500 and shop_price<=1000;

    not的用法:
    select good_id, cat_id from goods where cat_id not in (4, 5);
    select good_id, cat_id from goods where cat_id!=4 and cat_id != 5;

    模糊查询:‘ % ’:通配任意字符 '_':单个字符
    select good_id, cat_id from goods where good_name like '%诺基亚%';

    group by (要和聚合函数(统计函数)一起使用)
    作用:把行 按 字段 分组
    语法:group by col1, col2, ... colN
    运用场合:
    常见于统计场合,如按栏目计数帖子数,
    统计每个人的平均成绩等;

    max(shop_price) //聚合函数

    select good_id, good_name, max(shop_price) from goods; //语法是错误的
    select min(shop_price) from goods; //ok

    select cat_id, max(shop_price) from goods group by cat_id;

    select min(shop_price) from goods;

    select min(goods_id) from goods;

    select sum(goods_number) from goods;

    select avg(shop_price) from goods;

    计算表中函数
    select count(*) from goods;

    select cat_id, min(shop_price) from goods group by cat_id; //ok

    select cat_id, count(*) from goods group by cat_id;

    select good_id, good_name, market_price-shop_price from goods;


    select cat_id, sum(shop_price *good_number) from goods group by cat_id;

    给列取别名: as

    select cat_id, sum(shop_price*goods_number) as huokuan from goods group by cat_id;

    mysql连接查询: 

    select test1.id, test1.name, test1.address, test2.age from test1, test2 where test2.age=test1.id;
    等价于:(使用内连接)
    select test1.id, test1.name, test1.address, test2.age from test1 inner join test2 on test1.id=test2.age;

    inner join(内连接):
    inner join:如果表中有至少一个匹配,则返回行;
    left join:左连接,即使右表中没有匹配,也从左表返回所有的行;
    select test1.id, test2.name, test1.address, test2.age from test1 left join test2 on
    test1.id=test2.age;
    (右表: test2, 左表:test1)

    right join: 右连接,即使左表中没有匹配,也从右表返回所有的行;
    select test1.id, test1.name, test1.address, test2.age from test1 right join test2 on
    test1.id = test2.age;
    (右表:test2 左表:test1)

    full join: 只要其中一个表中存在匹配,就返回行;
    select test1.id, test1.name, test1.address, test2.age from test1 right join test2 on
    test1.id = test2.age;

    union:联合统计结果
    select name from test1 where age between 25 and 50 union select name from test2 where age<100;

  • 相关阅读:
    MVC vs SVC
    迪米特原则与接口隔离原则
    依赖倒置原理:依赖、稳定的抽象层
    【调侃】IOC前世今生
    Visual SVN Server启动失败0x8007042a错误
    syntax error, unexpected '['
    navicat将多个表导出为一个sql文件
    我的flashfxp左右界面怎么变成这样了?
    什么是国际顶级域名?
    什么是A记录/CNAME记录/MX记录/TXT记录
  • 原文地址:https://www.cnblogs.com/chris-cp/p/4190744.html
Copyright © 2011-2022 走看看