9

我没想到会这么难,但我试图在 MySQL 中设置一个用户变量来包含一个值数组。我不知道如何做到这一点,所以尝试做一些研究,但很惊讶没有找到答案。我努力了:

SET @billable_types = ['client1','client2','client3'];

原因是我想稍后在以下语句中使用该变量:

SELECT sum((time_to_sec(timediff(tlg.time_stop, tlg.time_start))/3600)) as billable_hours
    from mod_tmlog_time_log tlg, mod_tmlog_task_list mttl
    where date(tlg.time_start) >= @time_start
          and date(tlg.time_stop) <= @time_stop
          and mttl.type IN (@billable_types)
            and tlg.task_id = mttl.id
    group by start_date
    order by start_date desc;

非常感谢您的帮助。


快进了一会儿,我最终得到了以下快速而肮脏的解决方案,它没有给我在代码中其他地方重新使用数组的灵活性,但是嘿,这是一项不收费的管理任务,所以我不想再花更多的钱时间就可以了。

SELECT WEEKDAY(tlg.time_start) AS day_of_week, date(tlg.time_start) as start_date,
                            sum((time_to_sec(timediff(tlg.time_stop, tlg.time_start))/3600)) as billable_hours
                    from mod_tmlog_time_log tlg, mod_tmlog_task_list mttl
                    where date(tlg.time_start) >= @time_start
                          and date(tlg.time_stop) <= @time_stop
                          and mttl.type IN ('c1','c2','c3')
                            and tlg.task_id = mttl.id
                    group by start_date
                    order by start_date desc;

joostschouten 似乎找到了最优雅的解决方案(我自己还没有测试过),但是下次我写一些需要这个的东西时,我会记得测试它!

4

3 回答 3

12

刚刚在这里找到了答案:如何在 MySQL 中循环使用数组?

set @billable_types = 'client1,client2,client3';
select * from mttl where find_in_set(mttl.type, @billable_types);
于 2012-10-01T09:54:06.727 回答
0

正如 Marc B 提到的,MYSQL 中没有数组变量。

find_in_set解决方案的替代方法是使用SELECTUNION来模拟数组:

SELECT billable_type FROM (
  SELECT 'client1' AS billable_type UNION
  SELECT 'client2' AS billable_type UNION
  SELECT 'client3' AS billable_type) AS t

因此,您的查询将如下所示:

SELECT sum((time_to_sec(timediff(tlg.time_stop, tlg.time_start))/3600)) as billable_hours
     from mod_tmlog_time_log tlg, mod_tmlog_task_list mttl
     where date(tlg.time_start) >= @time_start
          and date(tlg.time_stop) <= @time_stop
          and mttl.type IN (

            SELECT billable_type FROM (
              SELECT 'client1' AS billable_type UNION
              SELECT 'client2' AS billable_type UNION
              SELECT 'client3' AS billable_type) AS t

          )
             and tlg.task_id = mttl.id
     group by start_date
     order by start_date desc;
于 2014-04-23T02:11:44.207 回答
0

如果用户具有 CREATE TABLE 权限,则可以通过创建一个临时的单列表来模拟一个数组。可以使用 SELECT 语句检索表中的一个或多个值。临时表在会话结束时被删除,但最好在不再需要它们时显式删除它们。

CREATE TEMPORARY TABLE billable_types (c VARCHAR(16));
INSERT INTO billable_types VALUES ('client1'), ('client2'), ('client3');

SELECT sum((time_to_sec(timediff(tlg.time_stop, tlg.time_start))/3600)) as billable_hours
from mod_tmlog_time_log tlg, mod_tmlog_task_list mttl
where date(tlg.time_start) >= @time_start
      and date(tlg.time_stop) <= @time_stop
      and mttl.type IN (SELECT * FROM billable_types)
        and tlg.task_id = mttl.id
group by start_date
order by start_date desc;

DROP TABLE billable_types;
于 2018-01-19T08:22:22.177 回答