MySQLクエリ文の簡単な操作例

3200 ワード

この実例は、MySQLクエリ文の簡単な動作を説明する。皆さんに参考にしてあげます。具体的には以下の通りです。
クエリー
データベース、データテーブルの作成

--      
create database python_test_1 charset=utf8;
--      
use python_test_1;
-- students 
create table students(
  id int unsigned primary key auto_increment not null,
  name varchar(20) default '',
  age tinyint unsigned default 0,
  height decimal(5,2),
  gender enum(' ',' ','  ','  ') default '  ',
  cls_id int unsigned default 0,
  is_delete bit default 0
);
-- classes 
create table classes (
  id int unsigned auto_increment primary key not null,
  name varchar(30) not null
);

データを用意する

--  students      
insert into students values
(0,'  ',18,180.00,2,1,0),
(0,'   ',18,180.00,2,2,1),
(0,'   ',29,185.00,1,1,0),
(0,'   ',59,175.00,1,2,1),
(0,'  ',38,160.00,2,1,0),
(0,'  ',28,150.00,4,2,1),
(0,'   ',18,172.00,2,1,1),
(0,'   ',36,NULL,1,1,0),
(0,'  ',27,181.00,1,2,0),
(0,'   ',25,166.00,2,2,0),
(0,'  ',33,162.00,3,3,1),
(0,'  ',12,180.00,2,4,0),
(0,'  ',12,170.00,1,4,0),
(0,'  ',34,176.00,2,5,0);

--  classes      
insert into classes values (0, "python_01 "), (0, "python_02 ");

すべてのフィールドをクエリー

select * from   ;

例:

select * from students;

クエリー指定フィールド

select  1, 2,... from   ;

例:

select name from students;

asを使ってフィールドにエイリアスを付けます。

select id as   , name as   , gender as    from students;

asで別名を付けることができます。

--               
select id, name, gender from students;
--   .   
select students.id,students.name,students.gender from students;
--      as       
select s.id,s.name,s.gender from students as s;

重複行を削除
selectの後ろの列の前にdistinctを使って重複行を削除できます。

select distinct  1,... from   ;

例:

select distinct gender from students;

MySQLに関する詳細について興味がある読者は、本駅のテーマを見ることができます。「MySQLクエリ技術大全書」、「MySQL常用関数大まとめ」、「MySQLログ操作テクニック大全」、「MySQL事務操作技術まとめ」、「MySQL記憶プロセス技術大全」および「MySQLデータベースのロックに関するスキルのまとめ
この記事が皆様のMySQLデータベース計に役立つことを期待します。