MySQL错误:使用的SELECT语句具有不同的列数

时间:2014-11-24 05:32:08

标签: mysql sql database join union

请查看以下查询..

SELECT Client_Portfolio.*,
Client.Name,
Provider.Name,
"One" AS Income_Type,
One.`One_Gross_Fee` AS "Gross_Fee",
One.`One_V_Fee` AS "V_Fee",
One.`One_E_Fee` AS "E_Fee",
One.`One_I_Fee` AS "I_Fee",
One.`One_Tax_Provision` AS "Tax_Provision",
One.`One_Net_Income` AS "Net_Income",
"N/A" AS VAT,
One.`Updated_Date`
FROM Client_Portfolio
INNER JOIN Portfolio ON Portfolio.`idPortfolio` = Client_Portfolio.`idPortfolio`
INNER JOIN Client ON Client.idClient = Client_Portfolio.idClient
JOIN Provider ON Provider.idProvider = Portfolio.idProvider
INNER JOIN One ON One.idPortfolio = Portfolio.idPortfolio

UNION

SELECT Client_Portfolio.*,
Client.Name,
Provider.Name,
"Two" AS Income_Type,
Two.`Two_Gross_Fee` AS "Gross_Fee",
Two.`Two_V_Fee` AS "V_Fee",
Two.`Two_E_Fee` AS "E_Fee",
Two.`Two_I_Fee` AS "I_Fee",
Two.`Two_Tax_Provision` AS "Tax_Provision",
Two.`Two_Net_Income` AS "Net_Income",
Two.`Two_Vat` AS VAT,
Two.`Updated_Date`
FROM Client_Portfolio
INNER JOIN Portfolio ON Portfolio.`idPortfolio` = Client_Portfolio.`idPortfolio`
INNER JOIN Client ON Client.idClient = Client_Portfolio.idClient
JOIN Provider ON Provider.idProvider = Portfolio.idProvider
INNER JOIN Two ON Two.idPortfolio = Portfolio.idPortfolio

UNION

SELECT 
Three.`Date` AS 'Updated_Date',
Three.`Gross_Fee` AS 'Gross_Fee',
Three.`Three_E_Fee` AS 'E_Fee',
Three.`Three_Tax_Provision` AS 'Tax_Provision',
Three.`Three_Net_Income` AS 'Net_Income',
Three.`Three_Vat` AS 'VAT',
0.0 AS 'V_Fee',
0.0 AS 'I_Fee',
Client.Name AS 'Client_Name',
'Three' AS Income_Type,
Provider.Name AS 'Provider_Name'
FROM Three
INNER JOIN Three_Obs ON Three_Obs.`idThree_Obs` = Three.`idThree_Obs`
LEFT JOIN Client ON Three_Obs.`idClient` = Client.`idClient`
LEFT JOIN Provider ON Three_Obs.`idProvider` = Provider.`idProvider`

当我运行此查询时,我收到错误The used SELECT statements have a different number of columns。这仅仅是因为上一次查询,它没有SELECT Client_Portfolio.*。在Client_Portfolio里面有2列,idClientidPortfolio,我没有理由将其加入到我的上一个查询中,因为它不需要。但是我尝试加入Three_Obs.idClient = Client_Portfolio.idClient只是为了看看它会是什么,而且我得到了完全无效的结果。

那么我该如何解决这个问题呢?我在上次查询中不需要Client_Portfolio.*,但我需要将该查询与其他2的查询UNION

1 个答案:

答案 0 :(得分:1)

由于列数必须与union语句匹配,因此您可以在最后一个语句中添加两个虚拟列并修复顺序,使其与union中的其他select语句匹配。试一试:

SELECT 
Client_Portfolio.idClient,
Client_Portfolio.idPortfolio,
Client.Name AS "Client_Name",
Provider.Name AS "Provider_Name",
"One" AS "Income_Type",
One.`One_Gross_Fee` AS "Gross_Fee",
One.`One_V_Fee` AS "V_Fee",
One.`One_E_Fee` AS "E_Fee",
One.`One_I_Fee` AS "I_Fee",
One.`One_Tax_Provision` AS "Tax_Provision",
One.`One_Net_Income` AS "Net_Income",
"N/A" AS "VAT",
One.`Updated_Date`
FROM Client_Portfolio
INNER JOIN Portfolio ON Portfolio.`idPortfolio` = Client_Portfolio.`idPortfolio`
INNER JOIN Client ON Client.idClient = Client_Portfolio.idClient
JOIN Provider ON Provider.idProvider = Portfolio.idProvider
INNER JOIN One ON One.idPortfolio = Portfolio.idPortfolio

UNION

SELECT 
Client_Portfolio.idClient,
Client_Portfolio.idPortfolio,
Client.Name AS "Client_Name",
Provider.Name AS "Provider_Name",
"Two" AS "Income_Type",
Two.`Two_Gross_Fee` AS "Gross_Fee",
Two.`Two_V_Fee` AS "V_Fee",
Two.`Two_E_Fee` AS "E_Fee",
Two.`Two_I_Fee` AS "I_Fee",
Two.`Two_Tax_Provision` AS "Tax_Provision",
Two.`Two_Net_Income` AS "Net_Income",
Two.`Two_Vat` AS "VAT",
Two.`Updated_Date`
FROM Client_Portfolio
INNER JOIN Portfolio ON Portfolio.`idPortfolio` = Client_Portfolio.`idPortfolio`
INNER JOIN Client ON Client.idClient = Client_Portfolio.idClient
JOIN Provider ON Provider.idProvider = Portfolio.idProvider
INNER JOIN Two ON Two.idPortfolio = Portfolio.idPortfolio

UNION

SELECT 
null AS idClient,
null AS idPortfolio,
Client.Name AS "Client_Name",
Provider.Name AS "Provider_Name",
"Three" AS "Income_Type",
Three.`Gross_Fee` AS "Gross_Fee",
0.0 AS "V_Fee",
Three.`Three_E_Fee` AS "E_Fee",
0.0 AS "I_Fee",
Three.`Three_Tax_Provision` AS "Tax_Provision",
Three.`Three_Net_Income` AS "Net_Income",
Three.`Three_Vat` AS "VAT",
Three.`Date` AS "Updated_Date"
FROM Three
INNER JOIN Three_Obs ON Three_Obs.`idThree_Obs` = Three.`idThree_Obs`
LEFT JOIN Client ON Three_Obs.`idClient` = Client.`idClient`
LEFT JOIN Provider ON Three_Obs.`idProvider` = Provider.`idProvider`

虽然,看看你的查询似乎做了什么,但我认为你可以重写它以至少使用连接到公共表客户端和提供程序,并稍微减少查询。