mySQL返回每个字段的特定行

时间:2017-11-06 13:02:19

标签: mysql sql

我会尽力解释这一点......我希望每个字段都能获得5个随机行。例如,我有:



SELECT "Name","Ticket ID","Channel"
FROM Tickets
  WHERE "Ticket Created at" between date('2017-09-01') AND date('2017-10-01')
  AND "Channel" = 'Phone' or 'Chat' or 'Email'
  AND "First Agent Primary Email" = 'abc.def@mail.com'




我想要做的是让桌子每个电话频道,每个聊天频道或每个电子邮件频道返回5个随机票证ID。

有人可以帮忙吗?

5 个答案:

答案 0 :(得分:2)

尝试此查询。

SELECT Name,Ticket ID,Channel FROM Tickets
WHERE Ticket Created at between date('2017-09-01') 
AND date('2017-10 
01')
AND Channel IN ( 'Phone', 'Chat' , 'Email')
AND First Agent Primary Email = 'abc.def@mail.com'
ORDER BY  RAND() LIMIT 5;

答案 1 :(得分:0)

我认为你会找到这样的东西:

var str = "There was no possibility of taking a walk that day. We had been wandering, indeed, in the leafless shrubbery an hour in the morning; but since dinner (Mrs. Reed, when there was no company, dined early) the cold winter wind had brought with it clouds so sombre, and a rain so";
str = str.split(' ').reduce((m, o) => {
    var last = m[m.length - 1];
    if (last && last.length + o.length < 30) {
        m[m.length - 1] = `${last} ${o}`;
    } else {
        m.push(o);
    }
    return m;
}, []);
console.log(str);

你可以看到我添加了行SELECT "Name","Ticket ID","Channel" FROM Tickets WHERE "Ticket Created at" between date('2017-09-01') AND date('2017-10-01') AND "Channel" = 'Phone' or 'Chat' or 'Email' AND "First Agent Primary Email" = 'abc.def@mail.com' ORDER BY RAND() LIMIT 5;,它随机排序行并返回前5行。

答案 2 :(得分:0)

你可能需要:

SELECT "Name","Ticket ID","Channel"
FROM Tickets
WHERE "Ticket Created at" between date('2017-09-01') AND date('2017-10-01')
AND ( "Channel" = 'Phone' or "Channel" = 'Chat' or "Channel" = 'Email' )
AND "First Agent Primary Email" = 'abc.def@mail.com'
ORDER BY RAND() LIMIT 5;

答案 3 :(得分:0)

当你使用复合名称时,不要使用列名称的引用   使用IN子句获取多个值(或重复每个或条件的列名)   你可以使用限制5和rand()的顺序获得5个休闲行

  SELECT Name,`Ticket ID`,Channel
  FROM Tickets
  WHERE `Ticket Created at` between date('2017-09-01') AND date('2017-10-01')
  AND `Channel` IN ( 'Phone', 'Chat' , 'Email')
  AND `First Agent Primary Email` = 'abc.def@mail.com'
  ORDER BY order by rand() limit 5 

答案 4 :(得分:0)

其他答案似乎都与他们的日期范围有问题,原始查询也是如此,所以我决定发布这个:

SELECT
    Name, `Ticket ID`, Channel
FROM Tickets
WHERE 
    `Ticket Created at` >= '2017-09-01' AND `Ticket Created at` < '2017-10-01' AND
    Channel IN ('Phone', 'Chat', 'Email') AND
    `First Agent Primary Email` = 'abc.def@mail.com'
ORDER BY
    RAND()
LIMIT 5;

关键是如果你想限制到9月份,你需要在使用日期文字时仔细说明。以下是要避免的陷阱:

WHERE `Ticket Created at` BETWEEN '2017-09-01' AND '2017-10-01'

这可能包括恰好落在10月1日午夜的数据,可能不是你想到的。而且,您不需要使用DATE包装日期文字;如果格式正确(使用正确的格式),MySQL已经可以处理它们了。

修改

如果您想为三个频道中的每个频道添加5张随机票,您可以使用联合查询:

SELECT Name, `Ticket ID`, Channel
FROM
(
    SELECT Name, `Ticket ID`, Channel
    FROM Tickets
    WHERE `Ticket Created at` >= '2017-09-01' AND
          `Ticket Created at` < '2017-10-01' AND
          Channel = 'Phone' AND
         `First Agent Primary Email` = 'abc.def@mail.com'
    ORDER BY RAND() LIMIT 5
) t1
UNION ALL
(
    SELECT Name, `Ticket ID`, Channel
    FROM Tickets
    WHERE `Ticket Created at` >= '2017-09-01' AND
          `Ticket Created at` < '2017-10-01' AND
          Channel = 'Chat' AND
         `First Agent Primary Email` = 'abc.def@mail.com'
    ORDER BY RAND() LIMIT 5
) t2
UNION ALL
(
    SELECT Name, `Ticket ID`, Channel
    FROM Tickets
    WHERE `Ticket Created at` >= '2017-09-01' AND
          `Ticket Created at` < '2017-10-01' AND
          Channel = 'Email' AND
         `First Agent Primary Email` = 'abc.def@mail.com'
    ORDER BY RAND() LIMIT 5
) t3;