3月29号Mysql查询

总结
**查询dang数据库中,d_book表中的所有字段:**SELECT *FROM d_book;

**查询d_book表中的id和author字段的值:**SELECT id,author FROM d_book;

**查询d_book表中出版社是北京十月文艺出版社的记录:**SELECT *FROM d_book WHERE publishing=‘北京十月文艺出版社’;

**查d_product表中dang_price在20以内的记录:**SELECT *FROM d_product WHERE dang_price <=20;

查询d_book表中作者的莫言、路遥、南派三叔或者二月河的记录:
SELECT *FROM d_book WHERE author IN(‘莫言’,‘路遥’,‘南派三叔’,‘二月河’);

查当当网上50-100元的书籍:*SELECT FROM d_product WHERE dang_price BETWEEN 50 AND 100;

**在d_book表中查所有姓张的作者记录:**SELECT *FROM d_book WHERE author LIKE(‘张%’);

在d_book表中查所有姓名是两个字的作者记录**:**SELECT *FROM d_book WHERE author LIKE(’__’);

查dang数据库中d_receive_address表中phone为空的记录:
SELECT *FROM d_receive_address WHERE phone=’’;

查d_book表中,作者是沈从文,并且出版社是北京十月文艺出版社的记录:
SELECT *FROM d_book WHERE author=‘沈从文’ AND publishing=‘北京十月文艺出版社’;

查d_book表中,作者是沈从文,或者出版社是北京十月文艺出版社的记录:
SELECT *FROM d_book WHERE author=‘沈从文’ OR publishing=‘北京十月文艺出版社’;

查d_book中的出版社有哪些,要求去重:
SELECT DISTINCT publishing FROM d_book;

查询当当网所有书籍的价格,按原价降序排列:
SELECT *FROM d_product ORDER BY dang_price DESC;

查询每个出版社为哪些作者出版过书籍:
SELECT publishing,GROUP_CONCAT(author) FROM d_book GROUP BY publishing;

查询d_category表中turn为1的记录,只显示前5条记录:
SELECT *FROM d_category WHERE turn=1 LIMIT 5;

查询d_category表中turn为1的记录,显示从第3条记录开始的4条记录:
SELECT *FROM d_category WHERE turn=1 LIMIT 2,4;

查询d_book表中有多少条记录:
SELECT COUNT(*) FROM d_book;

查d_product表中dang_price的最大值,最小值,总和,平均值:
SELECT MAX(dang_price),MIN(dang_price),SUM(dang_price),AVG(dang_price) FROM d_product;

查询员工的编号和姓名:
SELECT empno,ename FROM emp;

计算每个员工的年薪 要求显示员工编号 员工姓名和年薪:
SELECT empno ‘编号’,ename ‘姓名’,12*sal ‘年薪’ FROM emp;

计算每个员工的年收入 ;显示员工编号、员工姓名、年收入:
SELECT empno ename,12*(sal+IFNULL(comm,0)) FROM emp;
这里的‘IFNULL’是在表格中的值为NULL时排除取计算此NULL值

查询职位是clerk并且薪水小于1000 的员工:
SELECT *FROM emp WHERE job=‘clerk’ AND sal <=1000;

查询薪水在1600到3000的员工:
SELECT *FROM emp WHERE sal BETWEEN 1600 AND 3000;

查询没有津贴的员工信息:SELECT *FROM emp WHERE comm IS NULL;

查询有津贴的员工信息:SELECT *FROM emp WHERE comm IS NOT NULL;

查询职位是manager并且薪水大于2500的员工:SELECT *FROM emp WHERE job=‘manager’ AND sal >=2500;

查询职位是manager或者salesman的员工:
SELECT *FROM emp WHERE job=‘manager’ OR job=‘salesman’;

查询薪水大于1800,并且部门编号为20或30 的员工:
SELECT *FROM emp WHERE sal >=1800 AND deptno IN(20,30);

查询姓名以m开头的员工信息:
SELECT *FROM emp WHERE ename LIKE(‘m%’);

查询姓名以n结尾的员工信息:
SELECT *FROM emp WHERE ename LIKE(’%n’);

查询姓名中包含o的员工信息:
SELECT *FROM emp WHERE ename LIKE(’%o%’);

查询姓名中第二个字符为a的员工信息:
SELECT *FROM emp WHERE ename LIKE(’_a%’);

按薪水从小到大排序:
SELECT *FROM emp ORDER BY sal ASC;

查询manager的薪水,从高到底排序:
SELECT *FROM emp WHERE job=‘manager’ ORDER BY sal DESC;

按照职位和薪水排序:
SELECT *FROM emp job ORDER BY sal;

计算员工的平均薪水:
SELECT AVG(sal) FROM emp;

查有多少个职位:
SELECT COUNT(DISTINCT job) FROM emp;

查询每个职位的薪水合计,显示职位名称和薪水合计:
SELECT job,SUM(sal) FROM emp GROUP BY job;

列出平均工资大于2000的职位和平均工资:
SELECT job,AVG(sal) FROM emp GROUP BY job HAVING AVG(sal)>2000;

上一篇:oracle储存过程--存储过程


下一篇:Phonegap网络连接 HTML5