【问题标题】:MYSQL attendance by attendance dateMYSQL 按出勤日期出勤
【发布时间】:2017-11-20 15:18:28
【问题描述】:

我想用mysql做一个考勤系统。比如说两个不同的日期。 2017-12-21 和 2017-12-23

如果我有

我的表格测试看起来像这样。 姓名 日期 出席评论

Person 1 2017-12-21 Yes comment
Person 1 2017-12-23 Yes comment
Person 2 2017-12-21 No  comment 
Person 2 2017-12-23 Yes comment

而我想要的结果是

-----2017-12-21----------------2017-12-23-----------
-----Person 1 Yes comment ---- Person 1 Yes comment
-----Person 2 NO  comment ---- Person 2 Yes comment
----------------------------------------------------

认为 leftjoin 会起作用,但对我来说很难有人可以帮忙吗? 谢谢。

【问题讨论】:

  • 这似乎是为了解决家庭作业或培训问题。请展示对流程进行编码以获得响应的任何尝试。
  • 这是给运动队的。

标签: mysql dynamic-pivot


【解决方案1】:

我不太确定您期望的确切格式。如果你的表结构是这样的,

CREATE TABLE IF NOT EXISTS `attendance` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `date` date NOT NULL,
  `attendance` varchar(10) NOT NULL,
  `comment` varchar(1023) NOT NULL,
  PRIMARY KEY (`id`)
);

您可以运行以下查询,

SELECT date, 
GROUP_CONCAT(`name`, ' ', `attendance`, ' ', `comment`) AS attendance_info 
FROM `attendance`
GROUP BY `date`

以以下格式提取数据。

date        | attendance_info

2017-12-21  | Person 1 YES Comment,Person 2 NO Comment

2017-12-23  | Person 1 YES Comment,Person 1 YES Comment

【讨论】:

    【解决方案2】:

    它适用于运动队。我有这个我的数据库现在我已经测试了很多方法来做到这一点。 Group_Contact 不起作用。

    CREATE TABLE IF NOT EXISTS `attendance` (
          `playerid` int(11) NOT NULL,
          `idnumber` int(11) NOT NULL AUTO_INCREMENT,
          `name` varchar(255) NOT NULL,
          `date` date NOT NULL,
          `attendance` varchar(10) NOT NULL,
          `comment` varchar(1023) NOT NULL,
          PRIMARY KEY (`playerid`)
    );
    

    这是我以后想在 php 中使用它的结果。 (a = 出席 c = 评论)

    attendance| Name     | a2017-12-21|c2017-12-21|a2017-12-23|c2017-12-23|
    
              | Person 1 |     YES    |   Comment |     NO    |  Comment  |
    
              | Person 2 |     YES    |   Comment |    YES    |  Comment  |
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 2020-04-10
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多