MySQL の共通 OJ ルーチン

ここに画像の説明を挿入

MySQLクエリマニュアル:

1. よくあるキーワード:

優先順位付け:

from + on + join + where + group by + 結合関数 + had + select + unique + order by + limit offset + parition by

on & where: 同時に使用できる状況

  • 左右接続の致命的な点:

    左右を接続する場合、左の表の行が右の表の行に対応していない場合は、左の行+空の情報を保持ます

    culがnullではない場合、空の列を含む行をフィルタリングして除外することはできません。

  • 内部結合の利点:

    Nullを含む行を自動的にフィルタリングする

  • join on + where を同時に使用する場合:null 情報を使用する準備をする

    1. 結合後に null 値が含まれます
    2. null 値をフィルタリングする場所を使用する

持つもの: group by の優れたヘルパー

  • group by の後、すべての操作はデフォルトで各グループの最初の行に基づいて行われます。各グループの内部統計を入力したい場合は、

    select * from employees group by emp_no having count(salary) > 15;
    

入っている、入っていない: を含む、含まれていない

  • & =:

    //当明确后者的数据集中只有一个数据时,只能使用 = 
    //反之,数据集中有多个元素时使用in
    

個別に追加された場所:

  • 基本位置:選択後

    //select的执行优先级高于order by高于limit offset
    //所以只要 select 先筛一层,order by再排序,limit offset再筛一层,即可
    
  • 結合関数の場所: count() 内

    select distinct salary
    from employee e1
    where (select count(distinct salary) from employee e2 where e1.salary < e2.salary) = N-1;
    

グループ化とパーティション化の関係は次のとおりです。

  • 意味的に:
    1. グループ化後のデータ全体を強調します。各動作はグループの最初の動作に基づいています。
    2. パーティション化後の個々のデータを強調: 1 つの列をグループ化と、グループ内の他の列に応じたグループ内での並べ替えに使用します。
  • 構文的には:
    1. group by は独立して使用できます
    2. Partition by は組み込み関数でのみ使用でき、over() 内で使用できます。
  • group by の後、nonhaving 操作では各グループの最初の行インスタンスのみが表示されます。
    グループ化

意見:

  • 意味的に: サブテーブルを見つけて、それを保存するビューを作成します。これは参照に相当します。

    文法的に: 内部クエリサブテーブルの SQL ステートメントを追加する必要はありません。

    create view view_name as(
    	select * from table1 group by id order by score
    );
    

または、結合体:

  • or を使用するとインデックスが失敗し、データ量が多い場合には検索効率が悪くなります。
  • 通常、 または の代わりに Union を使用することをお勧めします。

合体():

  • パラメータ: 複数のパラメータ
  • 機能: 空ではない最初のパラメータを左から右に出力します。
  • 適用シナリオ:解雇特別判断

テーブル内の行の一時的な割り当て: ケース

  • 文法:

    select (
    case
        when mod(id, 2) == 1 and id != sum then id+1
        when mod(id, 2) == 1 and id == sum then id
        else id-1
    end
    ) student, id
    from seat, (select count(*) sum from seat) sum_table
    

2 つ目は、古典的なモデルです。

重複排除/異なる:

  • 方法 1: 説明

    select desc salary from salaries order by salary desc;
    
  • 法二:group by

    select salary from salaries group by salary order by salary desc;
    
  • グループ内外の関係者による:

    1. 内部: グループ内の複数の要素の支配的な検査を行う
    2. 外部: 結合が要素としてグループ全体のレビューを支配する場所を選択します。

最大値を満たす: 複数の行が取得される可能性があります

  • 最も価値のあるものの独自性を満たす: ソート

    select * from employees order by hire_date limit 1;
    
  • 最も多くの値を満たすことは一意ではありません: サブテーブルまたは = サブテーブル内

    select * from employees where hire_date in (select MAX(hire_date) from employees); 
    
    select * from employees where hire_date = (select MAX(hire_date) from employees); 
    

ランクn位:

  • Rank() over(): ソート結果はサブテーブルとして使用され、クエリに直接参加します。

    # rank()效果为 1 1 3
    select * from (select *,rank() over(order by score desc) as 'Rank' from table1) where Rank = n;
    
  • ense_rank() over(): ソート結果はサブテーブルとして使用され、クエリに直接参加します。

    # dense_rank()效果为 1 1 2
    select * from (select *,dense_rank() over(order by score desc) as 'Dense_Rank' from table1) where Dense_Rank = n;
    
  • row_number(): ソート結果はサブテーブルとして使用され、クエリに直接参加します。

    # row_number()效果为 1 2 3
    select * from (select *, row_number() over(order by score desc) as 'Row' from table1) where row_number = n;
    
  • select unique + order by: とサブテーブルクエリ

    select distinct score from table1 order by score limit 1 offset n-1;
    
  • group by + order by: group by の後にグループが 1 つだけ残り、order by よりも優先して実行されます。

    select score from table1 group by score order by score limit 1 offset n-1;
    
  • count + unique: プラスサブテーブルクエリ

    select distinct salary from employee e1
    where (select count(distinct salary) from employee e2 where e1.salary <= e2.salary)=N+1
    
  • count + group by: プラス結合テーブルクエリ

    select distinct e1.salary
    from employee e1, employee e2
    where e1.salary <= e2.salary
    group by e1.salary
    having count(distinct e2.salary) = N+1
    

グループ化、グループ内の情報のクエリ:

  • グループ内で統計情報を保有する:

    select *, count(course) from student group by id having count(course) >= 2;
    
  • group by と count が競合するかどうか:

    select title, count(title) s from titles group by title having s>1;
    				分组依据列可以是count参数
    # 虽然group by之后非having操作每组都是对组首操作,但是count()等函数是对整组操作
    				组内列也可以是count参数
    select title, count(emp_no) s from titles group by title having s>1;
    
  • サブテーブルとしてグループ化: サブテーブルクエリによって取得されたコンテンツは、グループの先頭であるため繰り返されません。

    select Email from Person
    where id in (select id from Person group by email having count(Email) > 1));
    

複数テーブル結合の NULL 情報:

  • XXX ではない行を見つける問題を解決するには、左と右の結合 + is null を使用します。

    select * from employee e left join depart d 
    on e.dep_no = d.dep_no and e.emp_no = d.emp_no
    where d.emp_no is null;
    
  • 効果は「ない」と同等です。

    select * from employee
    where emp_no is not in (select distinct emp_no from depart);
    
  • 左右の結合 + は null ではないため、セマンティクスをわずかに変更することでも問題を解決できる可能性があります。

    select * from employee e left join depart d 
    on e.dep_no = d.dep_no and e.emp_no != d.emp_no
    where d.emp_no is not null;
    
  • coalesce() と一緒に使用します。

    select s1.id id, coalesce(s2.student, s1.student);
    from seat s1 left join seat s2
    on ((s1.id + 1)^1)-1 = s2.id
    order by s1.id;
    

文字列内の文字の出現数

  • 文字列の連結:

    concat("MySQL中字符串连接");
    
  • 文字列の置換:

    replace(string, 'a', 'b');
    
  • 文字列の長さ:

    length();
    
  • 文字列内の出現数:

    select length(string)-length(replace(string, ',', ''))
    from strings;
    

同率および同率でないランキング:

  • 1 1 2:dense_rank()

    select dense_rank() over(order by socre) 'dense_rank'
    from table1;
    
  • 1 1 3:ランク()

    select rank() over(order by desc) 'rank'
    from table1;
    
  • 1 2 3:行番号()

    select rank() over(order by desc) 'row_number'
    from table1;
    

サブテーブル クエリと結合テーブル クエリの違いは次のとおりです。

  • サブテーブル クエリ: このテーブルの 1 行の情報を取得し、別のテーブルの行ごとに比較します。

    select * 
    from scores s1
    where (select count(distinct salary) from scores s2 where s1.score <= s2.score) = N;
    
  • 結合テーブル クエリ: 最初にデカルト積、次にフィルター、次に情報を抽出します。NULL 情報も特に便利です

    select * 
    from scores s1, scores s2
    where e1.salary <= e2.salary
    group by s1.id 
    having count(distinct e2.salary) = N;
    

おすすめ

転載: blog.csdn.net/buptsd/article/details/127475792