Mysql 出勤报告在 prepare stmt 上创建语法错误

Mysql attendence report creates syntax error on prepare stmt

我有两个表:一个只有一个名为 date_range 的日期列表(列名是日期),另一个名为 wp_wpdatatable_1,其中存储了所有日期(每次练习后,一个每个球员的行都是用球员的名字 (player)、练习日期 (date)、持续时间 (sporttrainingduration)、练习组 (practiceheldby) 等创建的...

现在我想创建一个报告。

我想要一个月中的每一天,第一列中的球员姓名以及球员参加训练的每一天我想列出他参加的训练(wp_wpdatatable_1.practiceheldby)

-- 1. Create an expression that builds the columns
set @sql = (
    select group_concat(distinct
        concat(
            "max(case when date_range.`date`='", date_range.`Date`, "' then `practiceheldby` end) as `", date_range.`Date`, "`"
        )
    )
    from wp_wpdatatable_1, date_range  
    where date_range.`Date`>=2019-06-01
              and date_range.`Date` <= 2019-06-07
);

-- 2. Complete the SQL instruction
set @sql = concat("select `player`, ", @sql , " from wp_wpdatatable_1 group by `player`");

-- 3. Create a prepared statement
PREPARE stmt from @sql;

-- 4. Execute the prepared statement
execute stmt;
DEALLOCATE PREPARE stmt;

我不是专业人士,我已经玩了三四天了。我想我已经很接近了,但我收到了这条错误信息:

从@sql准备 stmt MySQL meldet:文档

#1064 - Fehler in der SQL-Syntax。 Bitte die korrekte Syntax im Handbuch nachschlagen bei 'NULL' in Zeile 1

我错过了什么? 谢谢!

你非常接近。除了缺少日期引号外,代码第 5 行中的 case when date_range.date 应该是 case when date

-- 0. Toy data
CREATE TABLE date_range  (
  `Date` datetime
);
CREATE TABLE wp_wpdatatable_1  (
  `player` VARCHAR(5),
  `Date` DATE,
  `sporttrainingduration` FLOAT,
  `practiceheldby` VARCHAR(10)
);
INSERT INTO date_range 
VALUES
  ('2019-06-01'),
  ('2019-06-02'),
  ('2019-06-03')
  ;
INSERT INTO wp_wpdatatable_1 
VALUES
  ('AAA','2019-06-01','1','group1'),
  ('AAA','2019-06-02','2','group2'),
  ('AAA','2019-06-03','3','group3'),
  ('AAA','2019-06-04','1','gorup1'),
  ('BBB','2019-06-02','2','group2'), 
  ('CCC','2019-06-03','3','group3')
  ;
select * from date_range;
select * from wp_wpdatatable_1;
date_range
===================
Date
===================
2019-06-01 00:00:00
2019-06-02 00:00:00
2019-06-03 00:00:00

wp_wpdatatable_1
=======================================================
player  Date    sporttrainingduration   practiceheldby
=======================================================
AAA 2019-06-01  1   group1
AAA 2019-06-02  2   group2
AAA 2019-06-03  3   group3
AAA 2019-06-04  1   gorup1
BBB 2019-06-02  2   group2
CCC 2019-06-03  3   group3

更新后的代码如下:

-- 1. Create an expression that builds the columns
set @sql = (
    select group_concat(distinct
        concat(
            -- "max(case when date_range.`date`='", date_range.`Date`, "' then `practiceheldby` end) as `", date_range.`Date`, "`"
            "max(case when `date`='", date_range.`Date`, "' then `practiceheldby` end) as `", date_range.`Date`, "`"
        )
    )
    from wp_wpdatatable_1, date_range  
    -- where date_range.`Date`>=2019-06-01
              -- and date_range.`Date` <= 2019-06-07
    where date_range.`Date`>='2019-06-01'
              and date_range.`Date` <= '2019-06-07'
);

输出:

===========================================================================
player  2019-06-01 00:00:00     2019-06-02 00:00:00     2019-06-03 00:00:00
===========================================================================
AAA group1  group2  group3
BBB null    group2  null
CCC null    null    group3

Code here