【发布时间】:2014-07-09 13:35:34
【问题描述】:
执行此语句时遇到问题。
我在这里想要实现的是,我的结果将使用参数按条件排序。
例如,我想按升序对用户名进行排序,因此,在我的 Web 应用程序编码中,我有一个参数 @condition,当它读取“用户名”时,将执行下面的 sql 语句。
错误是:
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with date
出现了 6 次。
我不确定是否正确使用了 CASE。
我想要的结果是,当一个条件被调用并插入@condition 时,它应该对该特定条件的结果进行排序。
数据库中的属性是这样声明的:
C.joinDate - DATE
C.userName - VARCHAR(20)
C.firstName - VARCHAR(20)
C.lastName - VARCHAR(15)
C.contact - CHAR(8)
C.dob - DATE
C.userStatus - VARCHAR(8)
C.totalPoints - INT
R.resID - VARCHAR(8)
P.orderID - VARCHAR(8)
D.orderID - VARCHAR(8)
CR.securityCode - VARCHAR(10)
C.loginAttempted - INT
SELECT C.joinDate, C.userName, (C.firstName+' '+C.lastName) AS Name,
C.contact, C.dob, C.userStatus, C.totalPoints, COUNT(R.resID) AS Res,
COUNT(P.orderID) AS PreOrd, COUNT(D.orderID) AS DelOrd,
COUNT(CR.securityCode) AS Redeem, C.loginAttempted FROM Customer C
full join Reservation R ON C.userID = R.userID full join PreOrder P
ON R.resID = P.ResID full join DeliveryOrder D ON D.userID = C.userID
full join CustomerRedemption CR ON CR.userID = C.userID WHERE
(C.firstName+' '+C.lastName) LIKE '%%' and c.userName LIKE '%%' and
c.contact LIKE '%%' and c.userStatus LIKE '%%' GROUP BY C.joinDate,
C.userName, (C.firstName+' '+C.lastName), C.contact, C.dob,
C.userStatus, C.totalPoints, C.loginAttempted HAVING C.userName IS NOT
NULL ORDER BY CASE 'userName'
WHEN 'joinDate' THEN joinDate
WHEN 'userName' THEN userName
WHEN 'Name' THEN (C.firstName+' '+C.lastName)
WHEN 'dob' THEN dob
WHEN 'userSatus' THEN userStatus
WHEN 'totalPoints' THEN totalPoints
WHEN 'totalPoints' THEN totalPoints
WHEN 'Res' THEN COUNT(R.resID)
WHEN 'PreOrd' THEN COUNT(P.orderID)
WHEN 'DelOrd' THEN COUNT(D.orderID)
WHEN 'Redeem' THEN COUNT(CR.securityCode)
END
【问题讨论】:
-
你用的是什么数据库?
-
不管数据库如何,如果查询的结果要在数据库之外被某种类型的客户端使用,不要在数据库中排序。在客户端执行此操作要便宜得多。
标签: sql sorting sql-order-by case