从同一个表中加入数据

编程入门 行业动态 更新时间:2024-10-25 20:21:19
本文介绍了从同一个表中加入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个表,包含学生的姓名,电子邮件,职位等,以及他们的状态(可以是Y或N之一)。我想写一个查询,计算每个位置类型,以及每个类型中使用JOIN的Y的数量和N的数量。 (也就是说,它是一个包含三个列的表:Position,StatusIsYes和StatusIsNo。)

I have a table containing the names, emails, positions, etc of a students, as well as their "status" (which can be one of Y or N.) I want to write a query that counts the number of each type of position, as well as the number of Y AND the number of N within each type using JOIN. (That is, it would be a table with three columns: Position, StatusIsYes, and StatusIsNo.)

我已经使用CASE子句通过以下方式我不能弄清楚如何使用JOIN子句。

I have already done this using the CASE clause the following way, but I can't figure out how to do it using the JOIN clause.

SELECT position, COUNT(CASE WHEN status = 'Y' THEN 1 ELSE NULL END) AS StatusIsYes, COUNT(CASE WHEN status = 'N' THEN 1 ELSE NULL END) AS StatusIsNo FROM students GROUP BY crd

我感谢任何建议!

知道可以使用JOIN来完成,但是我想知道如何使用 a JOIN

I know it can be done without using JOIN, but I want to know how it is possible to do it with a JOIN.

推荐答案

您可以在要从同一个表中提取记录的情况下使用SELF JOIN。 例如: 表名:employee 字段:EmpId,EmpName,ManagerId 现在,如果您想获取Empolyees的详细信息在Manager Position中,我们需要这样编写查询: SELECT e1.EmpId,e1.EmpName FROM EmployeeDetails e1,EmployeeDetails e2其中e1.EmpId = e2.ManagerId;

You can use SELF JOIN in the case when you want to fetch records from same table. For ex: Table Name: employee Fields : EmpId,EmpName,ManagerId Now if you want to get the details of Empolyees who are in Manager Position for that we need to write query like this: SELECT e1.EmpId, e1.EmpName FROM EmployeeDetails e1, EmployeeDetails e2 where e1.EmpId=e2.ManagerId;

希望它会帮助你。

要了解更多信息,请检查 this 链接。

Fro more information please check this link.

更多推荐

从同一个表中加入数据

本文发布于:2023-10-17 06:18:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500016.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!