1

次のクエリを検討してください。

SELECT x.* FROM
(
    (
        SELECT (id,
            insertuserid AS transaction_user_id,
            (user_price * (-1)) AS amount,
            "INVOICE" AS transaction_type,
            insertdatetime
            )
        FROM invoice
        WHERE transaction_user_id = 4
    )
    UNION
    (
        SELECT (id,
            user_id AS transaction_user_id,
            amount,
            "PAYMENT" AS transaction_type,
            insertdatetime)
        FROM payment
        WHERE user_id = 4
    )
)
AS x
ORDER BY x.insertdatetime

AS最初に見たときのクエリエラー。

Unionの最初のに変更insertuserid AS transaction_user_idしても、次の行にある 2 番目の行でエラーになります: !!!insertuseridSELECTAS(user_price * (-1)) AS amount

エラーメッセージ:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near
'AS transaction_user_id, (user_price * (-1)) AS amount, "' at line 5


SELECT x.* FROM ( ( SELECT (id, insertuserid AS transaction_user_id,
(user_price * (-1)) AS amount, "INVOICE" AS transaction_type, insertdatetime )
FROM invoice WHERE transaction_user_id = 4 ) UNION
( SELECT (id, user_id AS transaction_user_id, amount,
"PAYMENT" AS transaction_type, insertdatetime) FROM payment
WHERE user_id = 4 ) ) AS x
ORDER BY x.insertdatetime

ありがとうございました

4

1 に答える 1

4

SELECT句の括弧を削除するだけで、

SELECT  x.* 
FROM
    (
        SELECT  id,
                insertuserid AS transaction_user_id,
                user_price * (-1) AS amount,
                'INVOICE' AS transaction_type,
                insertdatetime
        FROM    invoice
        WHERE   transaction_user_id = 4
        UNION
        SELECT  id,
                user_id AS transaction_user_id,
                amount,
                'PAYMENT' AS transaction_type,
                insertdatetime
        FROM    payment
        WHERE   user_id = 4
    ) AS x
ORDER BY x.insertdatetime
于 2013-03-16T09:43:57.480 に答える