有没有一种方法在MySQL中确定每次记录包含第一次使用的值?

时间:2016-12-20 03:52:16

标签: mysql sql return unique distinct

每次是第一次返回的部门名称时,我想返回部门名称周围的HTML。我的代码不在下面:

SELECT
Company.CompanyID,
Company.Bio,
Company.Email,
People.LastName,
People.FirstName,
Departments.DepartmentName AS DepartmentName,
Departments.DepartmentID
FROM
Company
LEFT JOIN People USING (CompanyID)
LEFT JOIN Departments
ON Departments.DepartmentID = Company.DepartmentID
ORDER BY
Departments.DepartmentID ASC,
People.LastName ASC,
People.FirstName ASC;

返回以下内容:

 +---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+
    |CompanyID| Bio            | Email      |LastName|FirstName|DepartmentName|DepartmentID| 
    +---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+
    | BK001   | Lorem Ipsum ...| e@email.com| Smith | Jay      | Finance     | 1 | 
    | BK002   | Dolor sit am ..| e@email.com| Rhays | Jim      | Marketing   | 2 | 
    | BK003   | Consectetur .. | e@email.com| Carrs | Sam      | IT          | 3 | 
    | BK004   | Sed do eiusm...| e@email.com| Webb  | Bob      | Marketing   | 2 | 
    | BK005   | Concept Phi... | e@email.com| Hart  | Rob      | Design      | 4 |

我希望它返回以下内容:

|CompanyID| Bio            | Email      |LastName|FirstName|HTML|DepartmentName|DepartmentID| 
 +---------+-------------------------------------+-------------+---------+--------+--------+------------+----------
| BK001   | Lorem Ipsum ...| e@email.com| Smith | Jay      |<div>|Finance     | 1 | 
| BK002   | Dolor sit am ..| e@email.com| Rhays | Jim      |<div>|Marketing   | 2 | 
| BK003   | Consectetur .. | e@email.com| Carrs | Sam      |<div>|IT          | 3 | 
| BK004   | Sed do eiusm...| e@email.com| Webb  | Bob      |     |Marketing   | 2 | 
| BK005   | Concept Phi... | e@email.com| Hart  | Rob      |<div>|Design      | 4 | 

3 个答案:

答案 0 :(得分:1)

使用x语句和Case

Sub-Query

注意:使用SELECT CASE WHEN CompanyID = (SELECT Min(C.CompanyID) FROM Departments D1 JOIN Company C ON d1.DepartmentID = c.DepartmentID WHERE D1.DepartmentName = Departments.DepartmentName) THEN '<div>' ELSE '' END AS HTML, ... FROM .. 名称来限定列,而不是编写完整的表名

答案 1 :(得分:0)

一个选项是添加一个额外的子查询来选择部门的第一次出现。我假设CompanyID表示按时间顺序将记录插入表中,因此这是首先出现的代码。

使用SELECT子句中的子查询可能会超出此选项。

SELECT t1.CompanyID,
       t1.Bio,
       t1.Email,
       t2.LastName,
       t2.FirstName,
       CASE WHEN t4.DepartmentID IS NOT NULL THEN '<div>' ELSE '' END AS HTML,
       t3.DepartmentName AS DepartmentName,
       t3.DepartmentID
FROM Company t1
LEFT JOIN People t2
    ON t1.CompanyID = t2.CompanyID
LEFT JOIN Departments t3
    ON t3.DepartmentID = t1.DepartmentID
LEFT JOIN
(
    SELECT d.DepartmentID, MIN(c.CompanyID) AS firstCompanyID    -- this subquery finds
    FROM Company c                                               -- the first appearance of
    LEFT JOIN Departments d                                      -- a company for a given
        ON c.DepartmentID = d.DepartmentID                       -- department
    GROUP BY d.DepartmentID
) t4
    ON t3.DepartmentID = t4.DepartmentID AND
       t1.CompanyID    = t4.firstCompanyID
ORDER BY t3.DepartmentID,
         t2.LastName,
         t2.FirstName;

注意:我们可以在MIN列上使用CompanyID,即文本,这是因为所有ID的宽度都固定为5。在这种情况下,词典排序将与数字的数字排序一致,这是我们真正关心的。请注意,如果此字段的宽度未修复,则可能无法使用此功能。

答案 2 :(得分:0)

检查这个。

    SET @row_number:=0;
    SET @DepartmentName:='';

    select 
    CompanyID,Bio,Email,LastName,FirstName,
    CASE When row_number ='1' then '<DIV>' else '' end as 'HTML',
    HTML
    DepartmentName,
    DepartmentID
    from
    (
    SELECT
    Company.CompanyID,
    Company.Bio,
    Company.Email,
    People.LastName,
    People.FirstName,
    Departments.DepartmentName AS DepartmentName,
    Departments.DepartmentID ,
    @row_number:=CASE WHEN @DepartmentName=DepartmentName THEN @row_number+1 ELSE 1 END AS row_number 
    FROM
    Company
    LEFT JOIN People USING (CompanyID)
    LEFT JOIN Departments
    ON Departments.DepartmentID = Company.DepartmentID
    ORDER BY
    Departments.DepartmentID ASC,
    People.LastName ASC,
    People.FirstName ASC;
    )a