zoukankan      html  css  js  c++  java
  • Mysql之SQL随笔

    1.创建数据库

    1 create database if not exists shop default character set utf8mb4 default collate utf8mb4_unicode_ci;

     2.查询数据库中重复数据

    select column,count(*) as count from table group by column HAVING count > 1
    

    3.依据关联表字段数据更新主表字段,并且限制更新条数

    UPDATE a
    INNER JOIN b ON b.order_id = a.id 
    SET a.target_id = b.target_id,
    a.item_type = b.type
    WHERE
    	a.id in (select id from (SELECT id from awhere target_id = 0 order by id limit 100) sub1);
    

    4.删除重复行,仅保留一行

    DELETE 
    FROM
    	table as ta
    WHERE
    	ta.id <> (
    	select t.maxid from (select max(tb.id) as maxid from table as tb where ta.order_id = tb.order_id) t
    	);
    

    执行时间会很长,可以对字段加索引

    5.select case赋值

    1 select name, (case sex when 0 then '未知' when 1 then '' when 2 then '') as '性别' from users;

    6.select concat字符串处理

    select title, concat('http://host/?u=', type) as url from address;
    7.update replace替换字段
    update table set url = replace(url, 'qq.com', 'aa.com');

    8.where json查询

    select * from table where json->'$.oid' = 2;

    9.update json字段

    update table set json = json_set(json, '$.oid', 3);

    update table set json = json_object('oid', 3);

     10.You can't specify target table '表名' for update in FROM clause

    翻译:不能先select出同一表中的某些值,再update这个表(在同一语句中)

    处理方法:将查询结果单独作为结果集

    update table set type = 1,type_symbol = 'aa' where order_id in (select id from (select orders.id from orders left join table on table.order_id = orders.id where orders.item_type = 2 and table.type != 1) t);

     11.查询表字段

    show full fields from table;

     12.表添加字段

    ALTER TABLE `database`.`table` 
    ADD COLUMN `column` tinyint(1) UNSIGNED NULL DEFAULT 1 COMMENT '备注' AFTER `column0`;

     13.结果集排序

    /*取积分最高的前10条数据*/
    SELECT id,nickname,score from users order by score desc limit 10;
    等效于
    SELECT id,nickname,score from users order by score desc limit 0, 10;
    此处可看到limit用法,limit i,n表示返回查询结果集中游标从i开始的n行数据(游标起始为0)。
    /*给结果集加上序号*/
    SELECT (@i:=@i+1) as rowno,id,nickname,score from users, (select @i:=0) as rowno order by score desc limit 0, 10;
    /*取第二行数据,limit 1, 1*/
    SELECT (@i:=@i+1) as rowno,id,nickname,total_revenue from users, (select @i:=1) as rowno order by total_revenue desc limit 1,1;
    /*取第三行数据,limit 2, 1*/
    SELECT (@i:=@i+1) as rowno,id,nickname,total_revenue from users, (select @i:=2) as rowno order by total_revenue desc limit 2,1;

     14.分数名次排名,相同的分数占据位次一样,后续连续不隔断

    增加一个变量,用于记录上一条数据的分数,只要当前数据分数跟上一条数据的分数比较,相同分数的排名就不变,不相同分数的排名就加一,并且更新变量的分数值为该条数据的分数,依次比较

    select Score, (select count(distinct(Score)) from Scores as t where t.Score >= s.Score) as `Rank` from Scores as s order by Score desc;

    以下sql不严谨,有误

    SELECT Score, (case when @currentScore = Score then @i when @currentScore := Score then @i := @i +1 end) as `rank` from test, (select @i := 0, @currentScore := null) as t order by Score desc;

     15.各种连接join

    https://www.cnblogs.com/fudashi/p/7491039.html

    inner join

    left join

    right join

    outer join

    full join

    cross join

    16.第N高的薪水,因为offset不支持运算,必须是定值,故提前得出游标

    CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    BEGIN 
      set N:=N-1;
      RETURN (
          select (select distinct Salary from Employee order by Salary desc limit 1 offset N) as SecondHightSalary
      );
    END

     17.连续出现的数字,查找所有至少连续出现三次的数字,结果去重

    select distinct(Num) as ConsecutiveNums from (select Num, case when @current = Num then @t:=@t+1 when @current := Num then @t:=1 end as times from Logs, (select @t:=1, @current:=null) as tmp) as tmp2 where times >= 3;

    SELECT DISTINCT
        l1.Num AS ConsecutiveNums
    FROM
        Logs l1,
        Logs l2,
        Logs l3
    WHERE
        l1.Id = l2.Id - 1
        AND l2.Id = l3.Id - 1
        AND l1.Num = l2.Num
        AND l2.Num = l3.Num
    ;
  • 相关阅读:
    有是JSF的一个小问题,搞了我两天!从周五到周二
    MyFaces Tree2控件使用 From http://blog.163.com/net_wood/blog
    使用JSF的Selectonemenu
    SSO摘抄
    用于快速将 Web 应用程序集成到 WebSphere Portal 中的选项
    Lua 公历转农历算法
    编程语言适用场合。。。
    了解grep、vim的查找 和正则表达式
    程序员知识资产的投资
    铁道部新客票系统设计(一)
  • 原文地址:https://www.cnblogs.com/caohongchang/p/11577385.html
Copyright © 2011-2022 走看看