如何在mysql中使用select case

时间:2013-10-15 09:44:20

标签: mysql

以下是MSSQL语法,如何将其转换为mysql

    SELECT          
    _nextBillingDate = UB.NextBillingDate,
    _billingFrequency = (CASE IFNULL(UB.BillingFrequency,0) WHEN 0 THEN
     _defaultBillingFrequency ELSE UB.BillingFrequency END),
    _isCompletelyCreditBilled = (CASE WHEN PO.ChargeAmount > 0 THEN 0 ELSE 1 END),
    _ownerId = PO.OwnerId   
    FROM PaymentOrder PO
    INNER JOIN UserBillingInfo UB ON PO.OwnerId = UB.OwnerId
    WHERE PO.Id = _paymentOrderId

2 个答案:

答案 0 :(得分:2)

在过程中,mysql使用select cols into vars from ...样式语法,所以试试这个:

-- DECLARE variables at the top of your procedure

SELECT
    UB.NextBillingDate,
    CASE IFNULL(UB.BillingFrequency,0) WHEN 0 THEN _defaultBillingFrequency ELSE UB.BillingFrequency END,
    CASE WHEN PO.ChargeAmount > 0 THEN 0 ELSE 1 END,
    PO.OwnerId
INTO
    _nextBillingDate, _billingFrequency, _isCompletelyCreditBilled, _ownerId 
FROM PaymentOrder PO
INNER JOIN UserBillingInfo UB ON PO.OwnerId = UB.OwnerId
WHERE PO.Id = _paymentOrderId

答案 1 :(得分:0)

在mysql中“选择*进入” 在mysql中不允许“选择*成”。这是解决此限制的示例:

创建表newtable SELECT * FROM oldtable