美文网首页
数据库 | MySQL | 4. 数据操作(除复杂查询)

数据库 | MySQL | 4. 数据操作(除复杂查询)

作者: ShadowFieldEric | 来源:发表于2020-12-17 09:09 被阅读0次

    查看表所有数据

    SELECT * FROM `website`;
    

    插入数据

    INSERT INTO `website`
    VALUES
    (null, 'Google', 'https://www.google.com', 1, 'USA'),
    (null, '淘宝', 'https://taobao.com', 13,'CN'),
    (null, '菜鸟教程', 'http://www.runoob.com', 4689, 'CN'),
    (null, '微博', 'http://weibo.com', 20, 'CN'),
    (null, 'Facebook', 'https://www.facebook.com', 3, 'USA'),
    (null, 'stackoverflow', 'https://www.stackoverflow.com', 0, 'IND');
    
    INSERT INTO apps
    VALUES
    (null, 'QQ App', 'http://im.qq.com','CN'),
    (null, '微博 App', 'http://weibo.com', 'CN'),
    (null, '淘宝 App', 'http://www.taobao.com', 'CN');
    
    INSERT INTO `runoob_tbl`
    VALUES
    ('1', '学习java', 'runoob.com', '2015-05-01 00:00:00'),
    ('2', '学习python', 'runoob.com', '2016-03-06 00:00:00'),
    ('3', '学习php', '菜鸟课程', '2017-04-12 00:00:00'),
    ('4', '学习mysql', '菜鸟课程', '2017-04-12 00:00:00'),
    ('5', '学习mysql-2', '李光明课程', NULL);
    

    删除

    (safe mode状态下需要加上id > 0)

    DELETE FROM `website` WHERE `name`='百度' AND `id` > 0;
    

    删除重复数据,且保留第一个数据

    DELETE FROM `website` WHERE `id` NOT IN (SELECT dt.min_id FROM (SELECT MIN(id) AS min_id, name FROM website GROUP BY name) dt);
    

    更新

    UPDATE `website` SET `alexa` = 50 WHERE `name` = '百度' AND `id` > 0;
    

    查询

    查询表中前100行的所有字段内容(0~99)

    SELECT * FROM `website` LIMIT 0,100;
    

    按需要查询字段内容 (默认前10000行即[LIMIT 0, 10000])

    SELECT `id`, `name`, `url` FROM `website`;
    SELECT `id` AS '编号',`name` AS '姓名', `url` AS '链接' FROM `website`;
    

    查询表中country字段的所有内容

    SELECT `country` FROM `website`;
    

    相关文章

      网友评论

          本文标题:数据库 | MySQL | 4. 数据操作(除复杂查询)

          本文链接:https://www.haomeiwen.com/subject/lksswktx.html