2025年5月22日 22:22:44 星期四

mysql数据库操作

就是这

http://7392072.blog.51cto.com/7382072/1325176

查询

  1. # 附带条件
  2. select * from qiushi_gif where source=20 and act_status<>7 order by id desc limit 10;
  3. # 多重条件
  4. select * from qiushi_gif where source=25 and created_at>'2017-04-16 17:50:42' and (act_status <> 7 or status='damage');
  5. # 查询数量
  6. select count(id) from qiushi_gif where source=20 and act_status<>7;

修改

  1. update qiushi_gif set status='publish' where id=XX;

删除

  1. delete from qiushi_gif where id = xx;
  2. # 删库跑路
  3. cat databases.txt | xargs -I XXX mysql -uroot -h10.0.8.45 -p'password' --default-character-set=utf8 -e "drop database XXX;"

创建表

普通表

  1. create table USER(
  2. id int not null primary key auto_increment,
  3. name char(20) not null default '',
  4. age int(4) not null default 0,
  5. is_admin int not null default 0,
  6. created_time datetime not null default now()
  7. );
  8. # 在远古时代也许是这样
  9. create table news(
  10. id bigint(20) not null primary key auto_increment,
  11. title varchar(120) not null default '',
  12. content text not null default '',
  13. author varchar(100) not null default '',
  14. clicks int not null default 0,
  15. status int(8) not null default 0,
  16. category varchar(100) not null default '',
  17. created_time timestamp not null default CURRENT_TIMESTAMP
  18. )ENGINE=InnoDB DEFAULT CHARSET=utf8;

关联表

  1. create table ARTICLE(
  2. id int not null primary key auto_increment,
  3. article_name char(20) not null default '',
  4. uid int not null default 0,
  5. article_pages int not null default 0,
  6. created_time datetime not null default now()
  7. );
  8. alter table article add constraint `user_article` foreign key (uid) references user(id) on delete cascade on update cascade;

级联

left join

  1. select news.id, news.title, news.created_time, pics.id as pid ,pics.url from news left join pics on (news.id = pics.nid) where news.created_time < %s and pics.status=3 limit %s;

修改表

  1. alter table MyClass add passtest int(4) default '0';

来自 大脸猪 写于 2017-04-27 17:04 -- 更新于2020-10-19 13:06 -- 0 条评论

0条评论

字体
字号


评论: