假设我从下面的查询得到以下结果,是否可以在结尾显示一个额外的列,同时为列的每一行分配随机名称。
如果可能的话,可以从sql查询中预先分配名称吗?如果答案为否,是否必须从单独的用户表中选择名称?
mysql> select DISTINCT(ticketmaskid), departmenttitle, ticketstatustitle, totalreplies, SEC_TO_TIME(firstresponsetime) as firstresponsetime, dateline from (select DISTINCT(t1.ticketmaskid) as ticketmaskid, t1.departmenttitle, t1.ticketstatustitle, t1.totalreplies, t1.firstresponsetime, from_unixtime(t1.dateline) as dateline from swtickets t1 join swticketauditlogs t2 on t1.ticketid = t2.ticketid where t1.departmenttitle = 'Support' and from_unixtime(t1.dateline) between '2014-03-19 18:00:00' and '2014-03-19 23:59:59' and t1.firstresponsetime = '0') a order by dateline desc;
+---------------+--------------------------+-------------------+--------------+-------------------+---------------------+ | ticketmaskid | departmenttitle | ticketstatustitle | totalreplies | firstresponsetime | dateline | +---------------+--------------------------+-------------------+--------------+-------------------+---------------------+ | PAF-798-78414 | Support | Open | 0 | 00:00:00 | 2014-03-19 21:00:05 | | QKX-440-27460 | Support | Open | 0 | 00:00:00 | 2014-03-19 20:23:25 | | YSY-607-89300 | Support | Open | 0 | 00:00:00 | 2014-03-19 19:30:01 | | OBF-585-91079 | Support | Open | 0 | 00:00:00 | 2014-03-19 19:18:11 | | JFL-571-34597 | Support | Open | 0 | 00:00:00 | 2014-03-19 19:14:36 | | UNV-310-75924 | Support | Open | 0 | 00:00:00 | 2014-03-19 19:11:02 | | BQI-734-94486 | Support | Open | 0 | 00:00:00 | 2014-03-19 19:08:26 | | FWF-601-76792 | Support | Open | 0 | 00:00:00 | 2014-03-19 18:33:56 | | HIL-530-30274 | Support | Open | 0 | 00:00:00 | 2014-03-19 18:30:02 | | GZR-272-44642 | Support | Open | 0 | 00:00:00 | 2014-03-19 18:08:09 | | MXY-801-80374 | Support | Open | 0 | 00:00:00 | 2014-03-19 18:03:49 | | UFX-287-74737 | Support | Open | 0 | 00:00:00 | 2014-03-19 18:00:06 | +---------------+--------------------------+-------------------+--------------+-------------------+---------------------+ 12 rows in set (0.16 sec) mysql>
我需要关注输出:
+---------------+--------------------------+-------------------+--------------+-------------------+---------------------+------------+ | ticketmaskid | departmenttitle | ticketstatustitle | totalreplies | firstresponsetime | dateline | Names | +---------------+--------------------------+-------------------+--------------+-------------------+---------------------+------------+ | PAF-798-78414 | Support | Open | 0 | 00:00:00 | 2014-03-19 21:00:05 | John | | QKX-440-27460 | Support | Open | 0 | 00:00:00 | 2014-03-19 20:23:25 | Michael | | YSY-607-89300 | Support | Open | 0 | 00:00:00 | 2014-03-19 19:30:01 | Maria | | OBF-585-91079 | Support | Open | 0 | 00:00:00 | 2014-03-19 19:18:11 | Jayme | | JFL-571-34597 | Support | Open | 0 | 00:00:00 | 2014-03-19 19:14:36 | John | | UNV-310-75924 | Support | Open | 0 | 00:00:00 | 2014-03-19 19:11:02 | Michael | | BQI-734-94486 | Support | Open | 0 | 00:00:00 | 2014-03-19 19:08:26 | Maria | | FWF-601-76792 | Support | Open | 0 | 00:00:00 | 2014-03-19 18:33:56 | Jayme | | HIL-530-30274 | Support | Open | 0 | 00:00:00 | 2014-03-19 18:30:02 | John | | GZR-272-44642 | Support | Open | 0 | 00:00:00 | 2014-03-19 18:08:09 | Michael | | MXY-801-80374 | Support | Open | 0 | 00:00:00 | 2014-03-19 18:03:49 | Maria | | UFX-287-74737 | Support | Open | 0 | 00:00:00 | 2014-03-19 18:00:06 | Jayme | +---------------+--------------------------+-------------------+--------------+-------------------+---------------------+------------- 12 rows in set (0.16 sec)
答案 0 :(得分:0)
如果您想要可重复的结果,则需要存储值。您应该在swtickets表中添加一列并运行更新或使用ticketmaskid和新列创建一个新表。
对于后者,这里有一个代码示例,它假设您创建一个包含两列(ticketmaskid& userid)的新表(ticket_person)。
您可以这样填充:
INSERT INTO ticket_person (ticketmaskid,userid)
SELECT swtickets.ticketmaskid
, FLOOR(lowvalue.minID + RAND()*(highvalue.maxID - lowvalue.minID))
FROM swtickets
CROSS JOIN (SELECT MIN(id) AS minID FROM usertable) as lowvalue
CROSS JOIN (SELECT MAX(id) AS maxID FROM usertable) as highvalue
LEFT JOIN ticket_person
ON swtickets.ticketmaskid = ticket_person.ticketmaskid
WHERE ticket_person.ticketmaskid IS NULL;
填充后,您可以通过JOIN获取您的值
SELECT swtickets.ticketmaskid
, COALESCE(usertable.name,CONCAT('Unknown userID:',ticket_person.userid),'Not Assigned')
FROM swtickets
LEFT JOIN ticket_person
ON ticket_person.ticketmaskid = swtickets.ticketmaskid
LEFT JOIN usertable
ON usertable.id = ticket_person.userid;
对于不可重复的结果,试试这个(我修改了你的查询 - 我不明白你是如何尝试使用DISTINCT和内部查询的目的)。如果您不想要CROSS JOIN,请在FLOOR函数中硬编码最小值和最大值。
SELECT DISTINCT t1.ticketmaskid, t1.departmenttitle, t1.ticketstatustitle
, t1.totalreplies ,SEC_TO_TIME(firstresponsetime) as firstresponsetime
, from_unixtime(t1.dateline) as dateline
, (SELECT name
FROM usertable
WHERE usertable.id = FLOOR(lowvalue.minID + RAND()*(highvalue.maxID - lowvalue.minID))) as `name`
FROM swtickets t1
JOIN swticketauditlogs t2
ON t1.ticketid = t2.ticketid
CROSS JOIN (SELECT MIN(id) AS minID FROM usertable) as lowvalue
CROSS JOIN (SELECT MAX(id) AS maxID FROM usertable) as highvalue
WHERE t1.departmenttitle = 'Support'
AND from_unixtime(t1.dateline) BETWEEN '2014-03-19 18:00:00' AND '2014-03-19 23:59:59'
AND t1.firstresponsetime = '0'
ORDER BY dateline DESC;