添加临时列以在单个mysql查询中用作标签

时间:2014-03-06 13:00:31

标签: mysql sql

我有以下mysql查询:

(
SELECT act_id, act_date, act_score
FROM actsWithScore
WHERE user_id =  '1'
)
UNION ALL (

SELECT act_id, vote_date, vote_score
FROM votes
WHERE user_voter =  '1'
)
ORDER BY  `act_date`

这很有效,输出类似于以下内容:

act_id      act_date                act_score
31          2014-03-06 07:08:12     NULL
30          2014-03-06 07:07:51     NULL
23          2014-03-06 06:52:54     1
21          2014-03-06 06:52:48     -1
12          2014-03-06 06:52:46     1
25          2014-03-06 06:52:42    -1

我想要做的是在输出上标识哪一行是行为(来自SELECT ... FROM actsWithScore查询),哪一行是投票(来自SELECT ... FROM votes查询),如下所示:

act_id      act_date                act_score    decription
31          2014-03-06 07:08:12     NULL         act
30          2014-03-06 07:07:51     NULL         act
23          2014-03-06 06:52:54     1            vote
21          2014-03-06 06:52:48     -1           act
12          2014-03-06 06:52:46     1            vote
25          2014-03-06 06:52:42    -1            vote

这可能,如果可行,怎么样?谢谢:))

2 个答案:

答案 0 :(得分:3)

(
SELECT act_id, act_date, act_score, 'act' as description
FROM actsWithScore
WHERE user_id =  '1'
)
UNION ALL 
(

SELECT act_id, vote_date, vote_score, 'votes' as description
FROM votes
WHERE user_voter =  '1'
)
ORDER BY  `act_date`

答案 1 :(得分:2)

就这样做

(
SELECT act_id, act_date, act_score, 'Act' As description
FROM actsWithScore
WHERE user_id =  '1'
)
UNION ALL (

SELECT act_id, vote_date, vote_score, 'Vote'  As description

FROM votes
WHERE user_voter =  '1'
)
ORDER BY  `act_date`