网站里面的图片做桌面不清晰度,做网站考什么赚钱,宽屏网站源码,免费素材网站设计1. 关联查询#xff08;JOIN#xff09; 关联查询用于从多个表中检索数据。它基于两个或多个表之间的共同字段#xff08;通常是主键和外键#xff09;来组合数据。 内连接#xff08;INNER JOIN#xff09;#xff1a; sql
SELECT a.name, b.order_date
FROM custome… 1. 关联查询JOIN 关联查询用于从多个表中检索数据。它基于两个或多个表之间的共同字段通常是主键和外键来组合数据。 内连接INNER JOIN sql
SELECT a.name, b.order_date
FROM customers a
INNER JOIN orders b ON a.customer_id b.customer_id; 这个查询会返回所有在orders表中有对应订单的客户名称和订单日期。 左连接LEFT JOIN sql
SELECT a.name, b.order_date
FROM customers a
LEFT JOIN orders b ON a.customer_id b.customer_id; 这个查询会返回所有客户的名称即使他们没有订单。对于没有订单的客户order_date字段将为NULL。 **右连接RIGHT JOIN和全连接FULL JOIN**也是常见的关联类型但使用较少。 2. 子查询Subquery 子查询是嵌套在另一个查询中的查询。它们可以用于在WHERE、FROM或SELECT子句中。 在WHERE子句中使用子查询 sql
SELECT name
FROM customers
WHERE customer_id IN (SELECT customer_id FROM orders WHERE order_date 2023-01-01); 这个查询会返回在2023年1月1日之后有订单的所有客户的名称。 在SELECT子句中使用子查询 sql
SELECT name, (SELECT COUNT(*) FROM orders WHERE orders.customer_id customers.customer_id) AS order_count
FROM customers; 这个查询会返回每个客户的名称和他们的订单数量。 3. 聚合查询Aggregate Queries 聚合查询使用聚合函数如SUM、AVG、COUNT、MAX、MIN来计算数据的统计信息。 sql
SELECT department, COUNT(*) AS employee_count, AVG(salary) AS average_salary
FROM employees
GROUP BY department; 这个查询会返回每个部门的员工数量和平均工资。 4. HAVING子句 HAVING子句用于过滤聚合查询的结果。它类似于WHERE子句但用于聚合函数的结果。 sql
SELECT department, AVG(salary) AS average_salary
FROM employees
GROUP BY department
HAVING AVG(salary) 50000; 这个查询会返回平均工资超过50000的部门。 5. 多表聚合查询 有时你可能需要对多个表进行聚合查询这通常涉及关联查询和聚合函数的组合。 sql
SELECT a.product_id, a.product_name, SUM(b.quantity) AS total_quantity_sold
FROM products a
JOIN sales b ON a.product_id b.product_id
GROUP BY a.product_id, a.product_name; 这个查询会返回每个产品的ID、名称和总销售量。 6. 窗口函数Window Functions 窗口函数允许你在查询的结果集中执行计算这些计算类似于聚合函数但它们是针对结果集的每一行进行的并且保留行的详细信息。 sql
SELECT employee_id, salary, AVG(salary) OVER (PARTITION BY department_id) AS avg_department_salary, RANK() OVER (ORDER BY salary DESC) AS salary_rank
FROM employees; 这个查询会返回每个员工的ID、薪水、他们所在部门的平均薪水和在整个公司中的薪水排名。 7. 递归查询Recursive Queries 递归查询用于处理层次结构数据如组织结构图、分类目录等。在SQL中这通常通过公用表表达式CTE来实现。 sql
WITH RECURSIVE EmployeeHierarchy AS ( SELECT employee_id, name, manager_id FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.employee_id, e.name, e.manager_id FROM employees e INNER JOIN EmployeeHierarchy eh ON e.manager_id eh.employee_id
)
SELECT * FROM EmployeeHierarchy; 这个查询会返回整个公司的员工层次结构从顶层管理者开始一直到每个 文章转载自: http://www.morning.bxyzr.cn.gov.cn.bxyzr.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.jwxmn.cn.gov.cn.jwxmn.cn http://www.morning.prplf.cn.gov.cn.prplf.cn http://www.morning.wnbpm.cn.gov.cn.wnbpm.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.bctr.cn.gov.cn.bctr.cn http://www.morning.xnfg.cn.gov.cn.xnfg.cn http://www.morning.brps.cn.gov.cn.brps.cn http://www.morning.gxqpm.cn.gov.cn.gxqpm.cn http://www.morning.atoinfo.com.gov.cn.atoinfo.com http://www.morning.xbdrc.cn.gov.cn.xbdrc.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.zsrdp.cn.gov.cn.zsrdp.cn http://www.morning.rlkgc.cn.gov.cn.rlkgc.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.txlnd.cn.gov.cn.txlnd.cn http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn http://www.morning.dwwbt.cn.gov.cn.dwwbt.cn http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.qtzqk.cn.gov.cn.qtzqk.cn http://www.morning.jcxgr.cn.gov.cn.jcxgr.cn http://www.morning.jcrfm.cn.gov.cn.jcrfm.cn http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn http://www.morning.qmnhw.cn.gov.cn.qmnhw.cn http://www.morning.zfzgp.cn.gov.cn.zfzgp.cn http://www.morning.nqbkb.cn.gov.cn.nqbkb.cn http://www.morning.mxmzl.cn.gov.cn.mxmzl.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.qpmmg.cn.gov.cn.qpmmg.cn http://www.morning.qyhcm.cn.gov.cn.qyhcm.cn http://www.morning.bzjpn.cn.gov.cn.bzjpn.cn http://www.morning.vattx.cn.gov.cn.vattx.cn http://www.morning.xyhql.cn.gov.cn.xyhql.cn http://www.morning.zzgkk.cn.gov.cn.zzgkk.cn http://www.morning.cltrx.cn.gov.cn.cltrx.cn http://www.morning.qggxt.cn.gov.cn.qggxt.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn http://www.morning.qtkdn.cn.gov.cn.qtkdn.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.kyfrl.cn.gov.cn.kyfrl.cn http://www.morning.wbhzr.cn.gov.cn.wbhzr.cn http://www.morning.wrlxt.cn.gov.cn.wrlxt.cn http://www.morning.grpfj.cn.gov.cn.grpfj.cn http://www.morning.prmyx.cn.gov.cn.prmyx.cn http://www.morning.tgbx.cn.gov.cn.tgbx.cn http://www.morning.bnlsd.cn.gov.cn.bnlsd.cn http://www.morning.lcxzg.cn.gov.cn.lcxzg.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.qrmry.cn.gov.cn.qrmry.cn http://www.morning.jqjnx.cn.gov.cn.jqjnx.cn http://www.morning.yjtnc.cn.gov.cn.yjtnc.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.rsqpc.cn.gov.cn.rsqpc.cn http://www.morning.ffgbq.cn.gov.cn.ffgbq.cn http://www.morning.lwmxk.cn.gov.cn.lwmxk.cn http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn http://www.morning.wmgjq.cn.gov.cn.wmgjq.cn http://www.morning.langlaitech.cn.gov.cn.langlaitech.cn http://www.morning.byywt.cn.gov.cn.byywt.cn http://www.morning.nypsz.cn.gov.cn.nypsz.cn http://www.morning.wfysn.cn.gov.cn.wfysn.cn http://www.morning.lysrt.cn.gov.cn.lysrt.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn http://www.morning.xkzr.cn.gov.cn.xkzr.cn http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.lchtb.cn.gov.cn.lchtb.cn http://www.morning.ngcsh.cn.gov.cn.ngcsh.cn