我有不同的sql,它返回不同的结果。我需要将它们全部列为列。但是我不知道该怎么办。有人可以帮我这个吗?这是我的sql下面>>>
SELECT "one" AS value_1,
"two" AS value_2
SELECT "three" AS value_3,
"four" AS value_4
SELECT "five" AS value_5,
"six" AS value_6
主要查询1>>>
SELECT -- lat.`group_info_id`, gi.`group_name`,
SUM(lat.`principal_outstanding`) AS principalOutstanding -- , SUM(lat.`accrued_interest`) AS interestReceivable, SUM(lat.`realizable_interest`) AS interestRealizable
-- , SUM(lat.`principal_outstanding`)+SUM(lat.`accrued_interest`)+SUM(lat.`realizable_interest`) AS loanDue, SUM(lat.`outstanding_balance`) AS totalLoanDue
FROM loan_account_transaction AS lat
INNER JOIN loan_account AS la ON (la.id = lat.`loan_account_id`)
LEFT JOIN group_info AS gi ON (gi.id = lat.`group_info_id`)
INNER JOIN
(
SELECT MAX(ilat.id) AS max_lat_id, ilat.loan_account_id
FROM loan_account_transaction AS ilat
WHERE ilat.`domain_status_id` = 1 AND ilat.`office_info_id` = (SELECT id FROM office_info WHERE office_code = 'UG500002')
AND ilat.`project_info_id` IN ('BI0000000000000000000004') AND ilat.transaction_date <= '2013-04-30'
GROUP BY ilat.loan_account_id
) AS tlat ON (tlat.loan_account_id = lat.`loan_account_id` AND tlat.max_lat_id = lat.id)
WHERE lat.`office_info_id` = (SELECT id FROM office_info WHERE office_code = 'UG500002')
AND lat.`project_info_id` IN ('BI0000000000000000000004')
AND lat.`loan_status_id` != 2 AND lat.`loan_status_id` != 7 AND la.`domain_status_id` = 1 AND lat.`domain_status_id` = 1
主要查询2&gt;&gt;&gt;
SELECT SUM(CASE WHEN la.loan_duration = 12 THEN 1 ELSE 0 END) AS total_count_40,
SUM(CASE WHEN la.loan_duration = 12 THEN IFNULL(la.disbursed_amount,0) ELSE 0 END) AS disbursed_amount_40,
SUM(CASE WHEN la.loan_duration = 6 THEN 1 ELSE 0 END) AS total_count_20,
SUM(CASE WHEN la.loan_duration = 6 THEN IFNULL(la.disbursed_amount,0) ELSE 0 END) AS disbursed_amount_20
FROM loan_account AS la
INNER JOIN member_info AS mi ON (la.disbursement_date <='2013-04-30'
AND la.project_info_id IN ('BI0000000000000000000004')
AND la.domain_status_id = 1 AND la.member_id=mi.id )
INNER JOIN office_info AS oi
ON( oi.id = 'BI0000000000000000000363' AND mi.branch_info_id = oi.id )
**新的&gt;&gt;&gt;
SELECT COUNT(1), SUM(principal_outstanding)
FROM
(
/* Current Loan */
SELECT tempData.country_id,
tempData.project_info_id,
tempData.prodId,
tempData.loan_name,
tempData.office_info_id,
office_info.office_code,
office_info.office_name,
tempData.group_info_id,
group_info.group_code,
group_info.group_name,
missed_days,
SUM(tempData.principal_outstanding) AS principal_outstanding,
COUNT(DISTINCT member_id) AS borrower_count
FROM (
SELECT lat.country_id,lat.office_info_id,lat.project_info_id, lp.id AS prodId, lp.`loan_name`,
lat.group_info_id,
lat.principal_outstanding,la.member_id,
(CEIL(lat.overdue_amount/la.installment_amount) *
CASE WHEN lpp.policy_repayments_plan_id = 1 THEN
1
WHEN lpp.policy_repayments_plan_id = 2 THEN
7
WHEN lpp.policy_repayments_plan_id = 3 THEN
30
WHEN lpp.policy_repayments_plan_id = 4 THEN
365
END) AS missed_days
FROM loan_account_transaction AS lat
INNER JOIN loan_account AS la ON (la.id = lat.loan_account_id)
INNER JOIN loan_product AS lp ON (la.`product_info_id` = lp.`id`)
INNER JOIN loan_product_policy AS lpp ON(lpp.id = la.policy_info_id)
INNER JOIN (SELECT MAX(lat2.id) AS max_lat2_id,lat2.loan_account_id FROM loan_account_transaction AS lat2
WHERE lat2.transaction_date <= '2013-03-31' AND
lat2.domain_status_id = 1 AND
lat2.country_id IN (1) AND
lat2.office_info_id IN ('BI0000000000000000000363') AND
lat2.project_info_id IN ('BI0000000000000000000004')
GROUP BY lat2.loan_account_id
) AS temp ON lat.id = temp.max_lat2_id
WHERE (lat.loan_status_id= 1 OR lat.loan_status_id IS NULL)
) AS tempData
INNER JOIN office_info ON (office_info.id = tempData.office_info_id)
LEFT JOIN group_info ON (group_info.id = tempData.group_info_id)
WHERE missed_days > 30
GROUP BY
tempData.country_id,
tempData.office_info_id,
tempData.project_info_id,
tempData.prodId,
tempData.group_info_id,
missed_days
UNION
/* Late and NIBL Loan */
SELECT tempData.country_id,tempData.project_info_id,
tempData.prodId,
tempData.loan_name,
tempData.office_info_id,office_info.office_code,office_info.office_name,
tempData.group_info_id,group_info.group_code,group_info.group_name,
tempData.missed_days,SUM(tempData.principal_outstanding) AS principal_outstanding,
COUNT(DISTINCT tempData.member_id) AS borrower_count
FROM
(SELECT lat.country_id,lat.office_info_id,lat.project_info_id, lp.id AS prodId, lp.`loan_name`,
lat.group_info_id,
(CASE WHEN lat.loan_status_id = 3 THEN
(la.loan_duration*30) -- +(la.loan_duration*30/2)
WHEN lat.loan_status_id = 4 THEN
(la.loan_duration*30) -- *2
WHEN lat.loan_status_id = 5 THEN
(la.loan_duration*30) -- *2+7
WHEN lat.loan_status_id = 6 THEN
(la.loan_duration*30) -- *2+7
END) AS missed_days,
lat.principal_outstanding,la.member_id
FROM loan_account_transaction AS lat
INNER JOIN loan_account AS la ON (la.id = lat.loan_account_id)
INNER JOIN loan_product AS lp ON (la.`product_info_id` = lp.`id`)
INNER JOIN (SELECT MAX(lat2.id) AS max_lat2_id,lat2.loan_account_id FROM
loan_account_transaction AS lat2
WHERE lat2.transaction_date <= '2013-03-31' AND
lat2.domain_status_id = 1 AND
lat2.country_id IN (1) AND
lat2.office_info_id IN ('BI0000000000000000000363') AND
lat2.project_info_id IN ('BI0000000000000000000004')
GROUP BY lat2.loan_account_id
) AS temp ON lat.id = temp.max_lat2_id
WHERE (lat.loan_status_id != 2 AND lat.loan_status_id != 7 AND lat.loan_status_id = 1)
) AS tempData
INNER JOIN office_info ON (office_info.id = tempData.office_info_id)
LEFT JOIN group_info ON (group_info.id = tempData.group_info_id)
GROUP BY tempData.country_id,
tempData.office_info_id,
tempData.project_info_id,
tempData.prodId,
tempData.group_info_id,
missed_days
) AS t
GROUP BY t.prodId
答案 0 :(得分:2)
您可以使用以下查询
select * from
(
(SELECT 'one' AS value_1, 'two' AS value_2) A
JOIN (SELECT 'three' AS value_3, 'four' AS value_4) B ON 1=1
JOIN (SELECT 'five' AS value_5, 'six' AS value_6) C ON 1=1
)
回答新问题:
我不确定这是否会起作用。但试试吧。
SELECT * from
(SELECT COUNT(1) COUNT1, SUM(principal_outstanding) SUM1
FROM
(
/* Current Loan */
SELECT tempData.country_id,
tempData.project_info_id,
tempData.prodId,
tempData.loan_name,
tempData.office_info_id,
office_info.office_code,
office_info.office_name,
tempData.group_info_id,
group_info.group_code,
group_info.group_name,
missed_days,
SUM(tempData.principal_outstanding) AS principal_outstanding,
COUNT(DISTINCT member_id) AS borrower_count
FROM (
SELECT lat.country_id,lat.office_info_id,lat.project_info_id, lp.id AS prodId, lp.`loan_name`,
lat.group_info_id,
lat.principal_outstanding,la.member_id,
(CEIL(lat.overdue_amount/la.installment_amount) *
CASE WHEN lpp.policy_repayments_plan_id = 1 THEN
1
WHEN lpp.policy_repayments_plan_id = 2 THEN
7
WHEN lpp.policy_repayments_plan_id = 3 THEN
30
WHEN lpp.policy_repayments_plan_id = 4 THEN
365
END) AS missed_days
FROM loan_account_transaction AS lat
INNER JOIN loan_account AS la ON (la.id = lat.loan_account_id)
INNER JOIN loan_product AS lp ON (la.`product_info_id` = lp.`id`)
INNER JOIN loan_product_policy AS lpp ON(lpp.id = la.policy_info_id)
INNER JOIN (SELECT MAX(lat2.id) AS max_lat2_id,lat2.loan_account_id FROM loan_account_transaction AS lat2
WHERE lat2.transaction_date <= '2013-03-31' AND
lat2.domain_status_id = 1 AND
lat2.country_id IN (1) AND
lat2.office_info_id IN ('BI0000000000000000000363') AND
lat2.project_info_id IN ('BI0000000000000000000004')
GROUP BY lat2.loan_account_id
) AS temp ON lat.id = temp.max_lat2_id
WHERE (lat.loan_status_id= 1 OR lat.loan_status_id IS NULL)
) AS tempData
INNER JOIN office_info ON (office_info.id = tempData.office_info_id)
LEFT JOIN group_info ON (group_info.id = tempData.group_info_id)
WHERE missed_days > 30
GROUP BY
tempData.country_id,
tempData.office_info_id,
tempData.project_info_id,
tempData.prodId,
tempData.group_info_id,
missed_days ) g1
GROUP BY g1.prodId )t1
/* Late and NIBL Loan */
JOIN (SELECT COUNT(1) COUNT2, SUM(principal_outstanding) SUM2
FROM(SELECT tempData.country_id,tempData.project_info_id,
tempData.prodId,
tempData.loan_name,
tempData.office_info_id,office_info.office_code,office_info.office_name,
tempData.group_info_id,group_info.group_code,group_info.group_name,
tempData.missed_days,SUM(tempData.principal_outstanding) AS principal_outstanding,
COUNT(DISTINCT tempData.member_id) AS borrower_count
FROM
(SELECT lat.country_id,lat.office_info_id,lat.project_info_id, lp.id AS prodId, lp.`loan_name`,
lat.group_info_id,
(CASE WHEN lat.loan_status_id = 3 THEN
(la.loan_duration*30) -- +(la.loan_duration*30/2)
WHEN lat.loan_status_id = 4 THEN
(la.loan_duration*30) -- *2
WHEN lat.loan_status_id = 5 THEN
(la.loan_duration*30) -- *2+7
WHEN lat.loan_status_id = 6 THEN
(la.loan_duration*30) -- *2+7
END) AS missed_days,
lat.principal_outstanding,la.member_id
FROM loan_account_transaction AS lat
INNER JOIN loan_account AS la ON (la.id = lat.loan_account_id)
INNER JOIN loan_product AS lp ON (la.`product_info_id` = lp.`id`)
INNER JOIN (SELECT MAX(lat2.id) AS max_lat2_id,lat2.loan_account_id FROM
loan_account_transaction AS lat2
WHERE lat2.transaction_date <= '2013-03-31' AND
lat2.domain_status_id = 1 AND
lat2.country_id IN (1) AND
lat2.office_info_id IN ('BI0000000000000000000363') AND
lat2.project_info_id IN ('BI0000000000000000000004')
GROUP BY lat2.loan_account_id
) AS temp ON lat.id = temp.max_lat2_id
WHERE (lat.loan_status_id != 2 AND lat.loan_status_id != 7 AND lat.loan_status_id = 1)
) AS tempData
INNER JOIN office_info ON (office_info.id = tempData.office_info_id)
LEFT JOIN group_info ON (group_info.id = tempData.group_info_id)
GROUP BY tempData.country_id,
tempData.office_info_id,
tempData.project_info_id,
tempData.prodId,
tempData.group_info_id,
missed_days
) AS g2
GROUP BY g2.prodId) t2 ON 1=1
答案 1 :(得分:0)
这可能是你的意思吗?
SELECT id, column1 AS data
FROM table1
UNION
SELECT id, column2 AS data
FROM table2
UNION
SELECT id, column3 AS data
FROM table3;
列column1
,column2
,column3
应该都具有相同的数据类型,并使它们具有相同的名称,以使UNION
能够正常工作。
答案 2 :(得分:0)
WITH cte ( value1, value2 ) AS ( SELECT 'ONE' AS value_1 , 'TWO' AS value_2 ), cte2 AS ( SELECT 'THREE' AS value_3 , 'FOUR' AS value_4 ), cte3 AS ( SELECT 'FIVE' AS value_5 , 'SIX' AS value_6 ) SELECT * FROM cte , cte2 , cte3