mysql常用命令

1、數據庫操作

create database db_name;  建庫

drop  database db_name;  刪庫

2、建立數據表

     先選擇數據庫

use db_name;  

create table `table_name`(

 order_id int(10) not null auto_increment primary key, 

 uid int(10) not null, 

 prodult_id int(10) not null

)engine=myisam default charset=utf8;

3、數據表操作

rename table`old_table_name` to`new_table_name`;

alter table `table_name` change column old_column_name new_column_name int(10) not null;

4、表數據操作

insert into `order` (uid,procuct_id) values(2,4),(2,3),(2,7);

insert into `order` (order_id,uid,product_id) values(0,4,4),(null,4,3);

insert into `order` values(0,3,4);

insert into `order` values(null,3,5);

insert into `order` set uid=4,product_id=4;


delete from `order` where order_id=12;

delete from `order`;


update `order` set product_id=2 where uid=1;


select * from `order` where uid=1 group by product_id having product_id>2 order by order_id desc;


5、經典題型

create table `user`(

 uid int(10) not null auto_increment primary key, 

 username varchar(20) not null, 

 passwd varchar(20) not null

)engine=myisam default charset=utf8;


create table `order`(

 order_id int(10) not null auto_increment primary key, 

 uid int(10) not null, 

 prodult_id int(10) not null

)engine=myisam default charset=utf8;

   (1)查詢訂單最多的用戶名

   select user.username from user left join (select uid,count(*) as c from `order` group by uid
    order by c desc) as O on user.uid=O.uid limit 1;


   select user.username from user right join (select uid,count(*) as c from `order` group by uid 

    order by c desc limit 1) as O on user.uid=O.uid;


   select user.username from (select uid,count(*) as c from `order` group by uid order by c desc limit 1) 

    as O left join user on user.uid=O.uid;


  select username from user where uid=(select uid from `order` group by uid order by count(*) desc limit 1)

 (2)查詢沒有下訂單的用戶

     select username from user where uid not in (select distinct uid from `order`);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章