MySQL语句基础

该笔记使用的数据库为MySQL-5.6.34,使用的建表语句为 :
/*
 Navicat MySQL Data Transfer

 Source Server         : 127.0.0.1
 Source Server Version : 50621
 Source Host           : localhost
 Source Database       : RUNOOB

 Target Server Version : 50621
 File Encoding         : utf-8

 Date: 05/18/2016 11:44:07 AM
*/

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
--  Table structure for `websites`
-- ----------------------------
DROP TABLE IF EXISTS `websites`;
CREATE TABLE `websites` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL DEFAULT '' COMMENT '站点名称',
  `url` varchar(255) NOT NULL DEFAULT '',
  `alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',
  `country` char(10) NOT NULL DEFAULT '' COMMENT '国家',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `websites`
-- ----------------------------
BEGIN;
INSERT INTO `websites` VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA'), ('2', '淘宝', 'https://www.taobao.com/', '13', 'CN'), ('3', '菜鸟教程', 'http://www.runoob.com/', '4689', 'CN'), ('4', '微博', 'http://weibo.com/', '20', 'CN'), ('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;
View Code
一、select语句
 
        1、select语句介绍
SELECT 语句用于从数据库中选取数据。结果被存储在一个结果表中,称为结果集。
        2、select语法
select column_name,column_name from table_name;
select * from table_name;
        3、举例说明
1.查看数据库有哪些:
mysql> show databases;
2.选择数据库:
mysql> use RUNOOB;
3.查看websites表中的所有信息
mysql> select * from websites;
4.从 "Websites" 表中选取 "name" 和 "country" 列
mysql> select name,country from websites;

二、SELECT DISTINCT 语句

        1、select distinc语句介绍
在表中,一个列可能会包含多个重复值,有时您也许希望仅仅列出不同(distinct)的值。
DISTINCT 关键词用于返回唯一不同的值。
        2、select distinc语法
select distinc cloumn_name.cloumn_name from table_name;
        3、举例说明
1.查询websites表中country字段不重复的值:
mysql> select distinct country from websites;
三、where语句
 
        1、where语句介绍
WHERE 子句用于提取那些满足指定标准的记录。
        2、where语法
select column_name,column_name from table_name where column_name operator valus;
        3、举例说明
1.查询websites表中country字段是‘CN’的:
mysql> select * from websites where country='CN';
 
文本字段 vs. 数值字段
SQL 使用单引号来环绕文本值(大部分数据库系统也接受双引号)。
在上个实例中 'CN' 文本字段使用了单引号。
如果是数值字段,请不要使用引号。
 
2.查询website表中id为1的记录
mysql> select * from websites where id=1;
3.从 "Websites" 表中选取国家为 "CN" 且alexa排名大于 "50" 的所有网站
mysql> select * from websites where country='CN' and alexa > 50;
4.从 "Websites" 表中选取国家为 "USA" 或者 "CN" 的所有客户
mysql> select * from websites where country='CN' or country='USA';
5.从 "Websites" 表中选取 alexa 排名大于 "15" 且国家为 "CN" 或 "USA" 的所有网站
mysql> select * from websites where alexa > 15 and (country='CN' or country='USA');
四、order by关键字
 
        1、order by介绍
ORDER BY 关键字用于对结果集进行排序。
ORDER BY 关键字用于对结果集按照一个列或者多个列进行排序。
ORDER BY 关键字默认按照升序对记录进行排序。如果需要按照降序对记录进行排序,您可以使用 DESC 关键字。
        2、语法
select column_name,column_name from table_name order by column_name asc|desc;
        3、举例说明
1.从 "Websites" 表中选取所有网站,并按照 "alexa" 列排序
mysql> select * from websites order by alexa;
2.从 "Websites" 表中选取所有网站,并按照 "alexa" 列降序排序
mysql> select * from websites order by alexa desc;
3.从 "Websites" 表中选取所有网站,并按照 "country" 和 "alexa" 列排序
mysql> select * from websites order by alexa,country;
五、insert into语句
 
        1、insert into介绍
INSERT INTO 语句用于向表中插入新记录。
        2、语法
insert into table_name values (value1,value2,value3,value4,...);
insert into table_name (column1,column2,column3,...) values (value1,value2,value3,...);
        3、举例说明
1.向 "Websites" 表中插入一个新行(主键id不能作为插入的内容,会有报错“1062 - Duplicate entry '4' for key 'PRIMARY'”)
mysql> insert into websites (name,url,alexa,country) values ('百度','https://www.baidu.com','56','CN';);
2.插入一个新行,但是只在 "name"、"url" 和 "country" 列插入数据(id 字段会自动更新)
mysql> insert into websites (name,url,country) values ('sohu','http://www.sohu.com','CN';);
六、update语句
 
        1、update语句介绍
UPDATE 语句用于更新表中已存在的记录。
        2、语法
update table_name set column_name1=value1,column2=value2,... where some_column=some_value;
        3、举例说明
1.把 "菜鸟教程" 的 alexa 排名更新为 5000,country 改为 USA
mysql> update websites set alexa='5000',country='USA' where name='菜鸟教程';

Update 警告!

所有的update语句都要带上where条件,否则很容易就会误修改一大批数据!!!
执行没有 WHERE 子句的 UPDATE 要慎重,再慎重。
七、delete语句
 
        1、delete语句介绍
DELETE 语句用于删除表中的记录。
        2、语法
delete from table_name where some_column=some_value;
        3、举例说明
WHERE 子句规定哪条记录或者哪些记录需要删除。如果您省略了 WHERE 子句,所有的记录都将被删除!
1.从 "Websites" 表中删除网站名为 "百度" 且国家为 CN 的网站
mysql> delete from websites where name='百度' and country='CN';
删除所有数据
可以在不删除表的情况下,删除表中所有的行。这意味着表结构、属性、索引将保持不变
1)delete from table_name;
2)delete * from table_name;
八、SELECT TOP, LIMIT, ROWNUM语句
 
        1、介绍
SELECT TOP 子句用于规定要返回的记录的数目。
SELECT TOP 子句对于拥有数千条记录的大型表来说,是非常有用的。
注释:并非所有的数据库系统都支持 SELECT TOP 子句。
        2、语法
SELECT column_name(s) FROM table_name LIMIT number;
        3、举例说明
1.从 "Websites" 表中选取头两条记录
mysql> select * from websites limit 2;
posted @ 2017-09-01 15:46  StaryJie  阅读(490)  评论(0编辑  收藏  举报