select查询

SELECT 语句的常用方法:

  • 基本方法
  • 查询条件(WHERE;=><;BETWEEN AND)
  • AND;OR;IN;OR IN;
  • 模糊查询(LIKE;’_’;’%’)
  • 对查询排序(ORDER BY:ASC;DESC)
  • SQL内置函数和计算(COUNT;AVG;SUM;MAX;MIN)
  • 子查询与连接查询
-- 子查询
SELECT of_dpt,COUNT(proj_name) AS count_project FROM project GROUP BY of_dpt
HAVING of_dpt IN
(SELECT in_dpt FROM employee WHERE name='Tom');

-- 连接查询
SELECT id,name,people_num
FROM employee,department
WHERE employee.in_dpt = department.dpt_name
ORDER BY id;
-- 等价于(on后的表名可省略)
SELECT id,name,people_num
FROM employee JOIN department
ON employee.in_dpt = department.dpt_name
ORDER BY id;

例:查询员工所在部门的人数及工程数

  • table1;table2;table3
    表一 & 表二
    表三
  • 查询方法1
    三表查询
    查询结果二
  • 查询方法2
    方法二

猜你喜欢

转载自blog.csdn.net/weixin_43650411/article/details/88172500