读书人

怎样给带distinct的查询结果加上字符串

发布时间: 2012-02-23 22:01:35 作者: rapoo

怎样给带distinct的查询结果加上字符串?
我的查询语句是这样的

SQL code
select distinct customer_code from purchase_customer_balance order by customer_code


我想在查询结果中添加字符串,如下
SQL code
select distinct '@'+customer_code'#' from purchase_customer_balance order by customer_code


但执行错误,提示如下:
如果指定了 SELECT DISTINCT,那么 ORDER BY 子句中的项就必须出现在选择列表中。

怎样解决这个问题?

另外如果我想添加的字符串不是 @ 而是 ' 怎样解决?

[解决办法]
SQL code
select distinct '@'+customer_code+'#' from purchase_customer_balance order by customer_code
[解决办法]
SQL code
select distinct '@'+customer_code + '#' from purchase_customer_balance order by 1or :select distinct '@'+customer_code + '#' from purchase_customer_balance order by '@'+customer_code + '#'
[解决办法]
SQL code
SELECT DISTINCT        '@' + customer_code + '#'FROM    purchase_customer_balanceORDER BY '@' + customer_code + '#'
[解决办法]
select distinct '@'+customer_code'#' from purchase_customer_balance order by '@'+customer_code'#'

读书人网 >SQL Server

热点推荐