Wednesday 26 September 2018

mysql - create, copy table

検索結果からテーブルを作成する

 create table price_result as
  select product, price,
  case
   when price > 100.0 then 'Level A'
   when price > 80.0 then 'Level B'
   else 'Level C'
  end as type
 from products;

テーブルをコピーする

  create table product_copy select * from products;

テーブルの構造のみコピーする

  create table product_empty like products;
  (構造を見てみる) desc product_empty;

関数を使ってみる

  select count(id) from products; ⇨id をカウントする
  select sum(price) from products;  ⇨合計
  select min(price) from products; ⇨最小
  select max(price) from products; ⇨最大
  select avg(price) from products; ⇨平均

重複したレコードを取り除いて取得
 select distinct product_name from products;
 select count(distinct product_name) from products;
 
↓来て頂きありがとうございます。クリックしていただくと、励みになります。↓
にほんブログ村 IT技術ブログ IT技術メモへ
にほんブログ村

No comments:

Post a Comment