简体   繁体   English

如何将 1 个表中的 2 个值加入 1 行?

[英]How to Join 2 value in 1 table into 1 row?

I have a table named log_attendance我有一个名为log_attendance的表

在此处输入图片说明

Output needed:需要的输出:

在此处输入图片说明

thank's for any help谢谢你的帮助

A simple approach to this, would be using GROUP BY clause by the emp_id column and then play with the aggregate methods MAX() and MIN() for get the desired result.一个简单的方法是通过emp_id列使用GROUP BY子句,然后使用聚合方法MAX()MIN()以获得所需的结果。 Note that, for mapping to NULL values you will need to do an extra check, like this:请注意,要映射到NULL值,您需要进行额外检查,如下所示:

SELECT
    emp_id,
    MIN(tap_in) AS tap_in,
    IF(MIN(tap_in) = MAX(tap_in), NULL, MAX(tap_in)) AS tap_out
FROM
    log_attendance
GROUP BY
    emp_id

You can check it here also: DB-Fiddle您也可以在这里查看: DB-Fiddle

You may have to use GROUP BY with MIN and MAX function to achieve your requirements.您可能必须使用GROUP BYMINMAX函数来实现您的要求。

SELECT emp_id,
       MIN(tap_in) AS tap_in,
       MAX(tap_in) tap_out
FROM TABLE
GROUP BY emp_id

Please see the below simple query请看下面的简单查询

SELECT empid,
   min([tap In]) [tap In] ,CASE WHEN  min([tap In])= max([tap In]) THEN NULL ELSE max([tap In]) END [tap out] 
FROM
    log_attendance 
 GROUP BY empid 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM