当前位置: 代码迷 >> Sql Server >> 怎样给带distinct的查询结果加上字符串?该怎么处理
  详细解决方案

怎样给带distinct的查询结果加上字符串?该怎么处理

热度:69   发布时间:2016-04-27 15:15:41.0
怎样给带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'#'

  相关解决方案