SELECT save AS 'Simpanan', owe AS 'Hutang'
FROM
(
SELECT t.amount AS save, NULL AS owe
FROM trans t JOIN category cat
ON t.category_id = cat.category_id
WHERE cat.category_id = 1
UNION ALL
SELECT NULL AS save, t.amount AS owe
FROM trans t JOIN category cat
ON t.category_id = cat.category_id
WHERE cat.category_id = 2
)
我一直在努力解决这个mysql查询问题。我想要实现的是,结果只有一行。目前,此查询输出2行,列中的某些字段为NULL。如何才能使它只输出1行:
Simpanan Hutang
500 200
答案 0 :(得分:2)
如果您确定只有一个结果使用子查询。
class StylesController < ApplicationController
def new
@style = Style.new
end
def create
# your code
end
end
或者:
SELECT
(SELECT t.amount AS save
FROM trans t JOIN category cat
ON t.category_id = cat.category_id
WHERE cat.category_id = 1) AS 'Simpanan',
(SELECT t.amount AS owe
FROM trans t JOIN category cat
ON t.category_id = cat.category_id
WHERE cat.category_id = 2) AS 'Hutang'