简体   繁体   中英

MySQL Error: The used SELECT statements have a different number of columns

Please have a look at the below query..

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`

When I run this query, I am getting the error The used SELECT statements have a different number of columns . That is simply because of the last query, it does not have the SELECT Client_Portfolio.* . Inside Client_Portfolio there are 2 columns, idClient and idPortfolio , I have no reason to join this to my last query, because it is not needed. However I tried to do join Three_Obs.idClient = Client_Portfolio.idClient just to see how it will be, and I got completely invalid results.

So how can I fix this issue? I have no need of Client_Portfolio.* in last query, but I need that query to be UNION with other 2.

Since the number of columns has to match for a union statement you can add two dummy columns in the last statement and fix the order so it matches the other select statements in the union. Give this a try:

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`

Although, looking at what your query seems to do I think you could rewrite it to use joins at least for the common tables client and provider and reduce the query a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM