开发者

Adding an Column Populated by an Arbitrary Value in SQL

Basically, I have a query that returns 2 columns:

SELECT ACTION_LOG_ID, COMMUNICATIONS_ID
FROM consumer_action_log
WHERE COMM_TYPE_ID=4

To every row in the result set for this query, I want to add the value 234 titled Customer_ID. Here was my best attempt:

SELECT ACTION_LOG_ID, COMMUNICATIONS_ID
FROM consumer_action_log
WHERE COMM_TYPE_ID=4
UNION 
SELECT CONSUMER_ID,CONSUMER_FIRST_NAME
FROM consumer_profile
WHERE CONSUMER_ID=234;

Note: the r开发者_如何学编程esult of the second half of the query will always be 234. I am using MySQL.


Try this:

SELECT 234 AS CONSUMER_ID, ACTION_LOG_ID, COMMUNICATIONS_ID 
  FROM consumer_action_log 
 WHERE COMM_TYPE_ID=4 


If I understand correctly, you want to add an extra COLUMN to every row, right? Here's how:

SELECT ACTION_LOG_ID, COMMUNICATIONS_ID, 234 'Customer_ID'
FROM consumer_action_log
WHERE COMM_TYPE_ID=4

or, alternate syntax

SELECT ACTION_LOG_ID, COMMUNICATIONS_ID, 234 AS Customer_ID
FROM consumer_action_log
WHERE COMM_TYPE_ID=4

What you tried (UNION) adds extra ROWS to your result set, not extra columns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜