显示数据库 USE DATABASE; 使用数据库 SHOW DATABASE; 显示数据库 SHOW TABLES; 显示表格 SHOW COULUMS FROM CUSTOMERS; 显示某列从某表 SHOW STATUS; 显示广泛的服务器状态信息 SHOW CREATE DATABASE; 显示创建特定数据库 SHOW CREATE TABLE; 显示创建特定表的SQL语句 SHOW GRANTS; 显示授权用户的安全权限 SHOW ERRORS; 显示服务器错误 SHOW WARNING; 显示警告消息 查询数据库 SELECT TABLENAME; 查询某个表 SELECT TABLENAME1,TABLENAME2,TABLENAME3 查询多个表从某库 FROM DATABASE; SELECT * FROM DATABASENAME; 查询全部列从某库 SELECT COLUMN 查询某列从某表 FROM TABLES; SELECT DISTINCT COLUMN 查询列中唯一值从某表 FROM TABLES; SELECT ROWNAME 查询某行从某表,取前5个 FROM TABLES LIMIT 5; SELECT ROWNAME 查询某行从某表,取第5行开始的5行 FROM TABLES LIMIT 5,5; 排序查询 SELECT TABLESNAME.COLUMN 选择某表的某列从某表 FROM TABLES; SELECT TABLESNAME.COLUMN 选择某表的某列从某库的某表 FROM DATABASE.TABLES SELECT COLUMN 选择某表从某列按照某列的顺序 FROM DATABASE ORDER BY COLUMN; SELECT COLUMN1,COLUMN2,C3 选择多列从某表按照某两列排序 FROM DATABASE ORDER BY COLUMN1,COLUMN2; SELECT COLUMN1,COLUMN2,C3 选择多列从某表按照某列降序排序 FROM DATABASE ORDER BY COLUMN1 DESC; SELECT COLUMN1,COLUMN2,C3 选择多列从某表按照某列降序排序后再按照某列顺序排序 FROM DATABASE ORDER BY COLUMN1 DESC,COLUMN2; SELECT COLUMN 查询某行从某表以某行排序取最值 FROM TABLES ORDER BY COLUMN1 DESC LIMIT 1; 按条件查询 SELECT COLUMN1,COLUMN2 查询值等于2.50的行 FROM TABLES WHERE COLUMN1=2.50; SELECT COLUMN1,COLUMN2 查询字符等于‘FUSES’的行 FROM TABLES WHERE COLUMN1='FUSES'; SELECT COLUMN1,COLUMN2 查询值小于10的行 FROM TABLES WHERE COLUMN1<10; SELECT COLUMN1,COLUMN2 查询值不等于1003的行 FROM TABLES WHERE COLUMN1<>1003; SELECT COLUMN1,COLUMN2 查询值不等于1003的行 FROM TABLES WHERE COLUMN1!=1003; SELECT COLUMN1,COLUMN2 查询值为5-10的行 FROM TABLES WHERE COLUMN1BETWEEN 5 AND 10; SELECT COLUMN1,COLUMN2 查询某列为空的行 FROM TABLES WHERE COLUMN1 IS NULL; SELECT COLUMN1,COLUMN2,C3 查询某列=1003且某列<=10的值 FROM TABLES WHERE COLUMN1=1003 AND COLUMN2<=10; SELECT COLUMN1,COLUMN2,C3 查询某列=1003或某列<=10的值 FROM TABLES WHERE COLUMN1=1003 OR COLUMN2<=10; SELECT COLUMN1,COLUMN2,C3 查询某列=1003或某列=1002且某列=10的值 FROM TABLES WHERE COLUMN1=1003 OR COLUMN2=1002 ADN C3>=10; SELECT COLUMN1,COLUMN2,C3 查询某列=1003或某列=1002,某列>=10的值 FROM TABLES (WHERE COLUMN1=1003 OR COLUMN2=1002 )ADN C3>=10;
SELECT COLUMN1,COLUMN2 查询多列从某表里的某列是10021003的所有行,按照某列排序 FROM TABLES WHERE COLUMN IN (1002,1003) ORDER BY COLUMN; SELECT COLUMN1,COLUMN2 查询多列从某表里的某列不是10021003所有行,按照某 列排序 FROM TABLES WHERE COLUMN NOT IN (1002,1003) ORDER BY COLUMN; 通配符查询 SELECT COLUMN1,COLUMN2 查询多列从某表里开头为JET的行 FROM TABLES WHERE COLUMN2 LIKE 'JET%'; SELECT COLUMN1,COLUMN2 查询多列从某表里结尾为JET的行 FROM TABLES WHERE COLUMN2 LIKE '%JET'; SELECT COLUMN1,COLUMN2 查询多列从某表里两侧为JT的行 FROM TABLES WHERE COLUMN2 LIKE 'J%T'; SELECT COLUMN1,COLUMN2 查询多列从某表里中间为JET的行 FROM TABLES WHERE COLUMN2 LIKE '%JET%';
SELECT COLUMN1,COLUMN2 查询多列从某表里结尾为TON ANBIL的行 FROM TABLES WHERE COLUMN2 LIKE '_TON ANBIL'; SELECT COLUMN1,COLUMN2 查询多列从某表里开头为TON ANBIL的行 FROM TABLES WHERE COLUMN2 LIKE 'TON ANBIL_'; 正则表达式 REGEXP SELECT COLUMN 按照列的顺序查询包含1000的列从某表 FROM TABLES WHERE COLUMN REGEXP‘1000’ ORDER BY COLUMN; SELECT COLUMN 按照列的顺序查询包含000的列从某表 FROM TABLES WHERE COLUMN REGEXP‘.000’ ORDER BY COLUMN; SELECT COLUMN 按照列的顺序查询包含100或200的列从某表 FROM TABLES WHERE COLUMN REGEXP‘100|200’ ORDER BY COLUMN; SELECT COLUMN 按照列的顺序查询包含1TON、2TON、3TON的列从某表 FROM TABLES WHERE COLUMN REGEXP‘【123】TON’ ORDER BY COLUMN; SELECT COLUMN 按照列的顺序查询包含1、2、3TON的列从某表 FROM TABLES WHERE COLUMN REGEXP‘1|2|3TON’ ORDER BY COLUMN;
SELECT COLUMN 按照列的顺序查询包含1TON-5TON的列从某表 FROM TABLES WHERE COLUMN REGEXP‘[1-5]TON’ ORDER BY COLUMN; SELECT COLUMN 按照列的顺序查询所有列表 FROM TABLES WHERE COLUMN REGEXP‘.’ ORDER BY COLUMN; SELECT COLUMN 按照列的顺序查询包含.的列从某表 FROM TABLES WHERE COLUMN REGEXP‘\\.’ ORDER BY COLUMN; SELECT COLUMN 按照列的顺序查询前边1-9,后边任意值的列 FROM TABLES WHERE COLUMN REGEXP‘\\([0-9]sticks?\\)’ ORDER BY COLUMN; SELECT COLUMN 按照列的顺序查询4位数字 FROM TABLES WHERE COLUMN REGEXP‘[[:digit:]]{4}’ ORDER BY COLUMN; 字段拼接 SELECT CONCAT(COLUMN1,'('COLUMN2,')') 拼接列1和列2 FROM TABLES ORDER BY COLUMN; SELECT CONCAT(RTRIM(COLUMN1),'(',RTRIM(COLUMN2),')') 取消空行的拼接 FROM TABLES ORDER BY COLUMN; SELECT CONCAT(RTRIM(COLUMN1),'(',RTRIM(COLUMN2),')')AS C3 合并列1和列2后作为C3 FROM TABLES ORDER BY COLUMN; SELECT COLUMN1, 查询列1×列2作为列4,某列结果为20005的行 COLUMN2, COLUMN3, COLUMN1*COLUMN2 AS COLUMN4 FROM TABLES WHERE COLUMN=20005 函数 SELECT COLUMN1,UPPER(COLUMN1)AS COLUMN2 将列1大写后作为列2 FROM TABLES ORDER BY COLUMN1 SELECT COLUMN1,COLUMN2 近似值匹配 FROM TABLES WHERE SOUNDEX(COLUMN2)=SOUNDEX('Y lie'); SELECT COLUMN1,COLUMN2 查询列3为2000-01-02的行 FROM TABLES WHERE COLUMN3='2000-01-02' SELECT COLUMN1,COLUMN2 查询列3为2000-01-02和2000-03-01的行 FROM TABLES WHERE YEAR(COLUMN3)BETWEEN '2000-01-02'AND'2000-03-01' SELECT AVG(COLUMN1) AS COLUMN2 查询列1的平均数作为列2 FROM TABLES SELECT COUNT(*)AS COLUMN2 查询列的总行数 FROM TABLES SELECT COUNT(COLUMN1)AS COLUMN2 查询列1的总行数作为列2 FROM TABLES SELECT MAX(COLUMN1)AS COLUMN2 查询列1的最大值作为列2 FROM TABLES SELECT MIN(COLUMN1)AS COLUMN2 查询列1的最小值作为列2 FROM TABLES SELECT SUM(COLUMN1)AS COLUMN2 查询列1中列3为20005的和作为列2 FROM TABLES WHERE COLUMN3=20005 SELECT SUM(COLUMN1*COLUMN2)AS COLUMN3 查询列1乘列2的和作为列3 FROM TABLES WHERE COLUMN3=20005 SELECT AVG(DISTINCT COLUMN1)AS COLUMN2 查询列1中的唯一值作为列2 FROM TABLES WHERE COLUMN3=20005 SELECT COUNT(*)AS COLUMN2 分别查询 MIN(COLUMN1) AS COUNTMN1-1 MAX(COLUMN2) AS COUNTMN1-2 AVG(COLUMN3) AS COUNTMN1-3 FROM TABLES; SELECT COUNT (*)AS COLUMN1 从表里查询所有某列等于1003的行作为列1 FROM TABLES WHERE COLUMN=1003 SELECT COLUMN1 COUNT (*)AS COLUMN2 从表里查询所有某列等于1003的行作为列1 FROM TABLES GROUP BY COLUMN=1003 SELECT COLUMN1 COUNT (*)AS COLUMN2 从表里查询所有某列计数大于等于的行作为列2 FROM TABLES GROUP BY COLUMN1 HAVING COUNT(*)>=2; SELECT COLUMN1 COUNT (*)AS COLUMN2 列3>=10,列1>=2 FROM TABLES WHERE COLUMN3>=10 GROUP BY COLUMN1 HAVING COUNT(*)>=2; SELECT COLUMN1 COUNT (*)AS COLUMN2 FROM TABLES GROUP BY COLUMN1 HAVING COUNT(*)>=2; SELECT COLUMN1,SUM(COLUMN2*COLUMN3)AS COLUMN4 选择列1、和作为列2 FROM COLUMN4 GROUP BY COLUMN1 HAVING SUM(COLUMN2*COLUMN3)>=50 ORDER BY COLUMN4 SELECT COLUMN1 查询所有C3为TNT2的列 FROM COLUMN2 WHERE COLUMN3='TNT2' SELECT COLUMN1 查询所有C3为102、103的列 FROM COLUMN2 WHERE COLUMN3 IN (102,103) SELECT COLUMN1 查询所有C3为102,103的列 FROM COLUMN2 WHERE COLUMN3 IN (SELECT COLUMN1 FROM COLUMN2 WHERE COLUMN4 IN 'TNT2') SELECT COLUMN1 查询所有C3为102,103的列 FROM COLUMN2 WHERE COLUMN3 IN (SELECT COLUMN1 FROM COLUMN2 WHERE COLUMN4 IN (SELECT COLUMN1 FROM COLUMN2 WHERE COLUMN4 IN 'TNT2')) 联结表 SELE COLUMN1,COLUMN2,COLUMN3 查询匹配表1列1和表2列2相对应的两列 FROM TABLES1,TABLES2 WHERE TABLES1.COLUMN1=TABLES2.COLUMN2 ORDER BY COLUMN1,COLUMN2; SELE COLUMN1,COLUMN2,COLUMN3 查询匹配表1列1和表2列2相对应的两列 FROM TABLES1,TABLES2 ORDER BY COLUMN1,COLUMN2;
SELECT COLUMN1,COLUMN2,COLUMN3 FROM TABLES1 INNER JOIN TABLES2 ON TABLES.COLUMN1=TABLES2.COLUMN1
SELECT COLUMN1,COLUMN2,COLUMN3,COLUMN4 FROM TABLES1,TABLES2,TABLES3 WHERE TABLES2.COLUMN1=TABLES
SELECT COLUMN1,COLUMN2,COLUMN3,COLUMN4 FROM TABLES1,TABLES2,TABLES3 WHERE TABLES. SELECT COLUMN1,COLUMN2 查询 FROM TABLES1,TABLES2,TABLES3 WHERE TABLES1.COLUMN1=TABLES2.COLUMN2 AND TABLES3.COLUMN1=TABLES2.COLUMN2 AND COLUMN3='TAN2'; 高级联结 SELECT CONCAT(RTRIM(COLUMN1),'('RTRIM(COLUMN2),')')AS COLUMN2 FROM TABLES ORDER BY COLUMN1
SELECT COLUMN1,COLUMN2 FROM TABLES AS C,TABLES2 AS O,TABLES3 AS OI WHERE C.COLUNM3=O.COLUMN3 AND OI.COLUMN3=O.COLUMN4 AND COLUMN5.'TNT2';
SELECT COLUMN1,COLUMN2 FROM TABLES1 WHERE COLUMN3=(SELECT COLUMN3 FROM TABLES1 WHERE COLUMN1='DTNTR');
SELECT TABLES1.COLUMN1,TABLES1.COLUMN2 FROM TABLES2 AS TABLES1,TABLES AS COLUMN2 WHERE TABLES1.COLUMN1=TABLES2.COLUMN2 AND TABLES2.COLUMN2='DTNTR'
SELECT TABLES.*,TABLES1.COLUMN1,TABLES1.COLUMN2 FROM TABLES2 AS TABLES1,TABLES AS COLUMN2 WHERE TABLES1.COLUMN1=TABLES2.COLUMN2 AND TABLES2.COLUMN2='DTNTR'
SELECT TABLES1.COLUMN1, TABLES1.COLUMN2, COUNT(TABLES.COLUMN3) AS COLUMN3 FROM TABLES1 INNER JOIN TABLES1 ON TABLES1.COLUMN1=TABLES.COLUMN2 GROUP BY TABLES1.COLUMN2
SELECT TABLES1.COLUMN1, TABLES2.COLUMN2, COUNT(TABLES.COLUMN)AS COLUMN FROM TABLES
SELECT COLUMN1,COLUMN2,COLUMN3 FROM TABLES1 WHERE COLUMN3<=5;
SELECT COLUMN1,COLUMN2,COLUMN3 FROM TABLES1 WHERE COLUMN3 IN (1001,1002);
SELECT COLUMN1,COLUMN2,COLUMN3 FROM TABLES1 WHERE COLUMN3<=5 UNION SELECT COLUMN1,COLUMN2,COLUMN3 FROM TABLES1 WHERE COLUMN3 IN (1001,1002);
SELECT COLUMN1,COLUMN2,COLUMN3 FROM TABLES1 WHERE COLUMN3<=5 UNION SELECT COLUMN1,COLUMN2,COLUMN3 FROM TABLES1 WHERE COLUMN3 IN (1001,1002) ORDER BY COLUMN1,COLUMN2;
SELECT COLUMN1 FROM TABLES1 WHERE MATCH(COLUMN1)AGAINST('RABBIT')
SELECT COLUMN1 FROM TABLES1 WHERE COLUMN1 LIKE '%RABBIT%';
SELECT COLUMN1 MATCH(COLUMN1)AGAINST('RABBIT')AS RANK FROM TABLES;
SELECT COLUMN1 FROM TABLES WHERE MATCH(COLUMN1)AGAINST('ANVILS');
SELECT COLUMN1 FROM TABLES WHERE MATCH(COLUMN1)AGAINST('ANVILS'WITH QUERY EXPANSION);
SELE COLUMN1 FROM TABLES WHERE MATCH(COLUMN1)AGAINST('HEAVY'IN BOOLEAN MODE);