Reputation: 13
I have an error "the used select statements have a different number of columns mysql"
My query
SELECT
* FROM
inventory
WHERE
barcode1 = 'DR963560860GYYQBKBBD2'
OR barcode2 = 'DR963560860GYYQBKBBD2'
OR barcode3 = 'DR963560860GYYQBKBBD2'
OR barcode4 = 'DR963560860GYYQBKBBD2' UNION
SELECT
p.id AS 'PartId',
m.`Name` AS 'Model',
c.`Name` AS 'Color',
pt.`Name` AS 'PartType'
FROM
inventory i
JOIN part p ON p.Id = i.PartId
JOIN model m ON m.Id = p.ModelId
JOIN color c ON c.Id = p.ColorId
JOIN parttype pt ON pt.Id = p.PartTypeId
WHERE
barcode1 = @barcode
OR barcode2 = @barcode
OR barcode3 = 'DR963560860GYYQBKBBD2'
OR barcode4 = 'DR963560860GYYQBKBBD2';
What i want to get is all the data in inventory table in the first select statement, and another one is from another table.
Before, I have two queries which is
SELECT
*
FROM
inventory
WHERE
barcode1 = @Barcode
OR barcode2 = @Barcode
OR barcode3 = @Barcode
OR barcode4 = @Barcode`
that will retrieve all the information in inventory and the other one is
SELECT
p.id AS 'PartId',
m.`Name` AS 'Model',
c.`Name` AS 'Color',
pt.`Name` AS 'PartType'
FROM
inventory i
JOIN part p ON p.Id = i.PartId
JOIN model m ON m.Id = p.ModelId
JOIN color c ON c.Id = p.ColorId
JOIN parttype pt ON pt.Id = p.PartTypeId
WHERE
barcode1 = @Barcode
OR barcode2 = @Barcode
OR barcode3 = @Barcode
OR barcode4 = @Barcode
That will load just the four columns.
Since my output load too slow, i will combine it in one query. Is this possible? How can I do that?
I'm a beginner in MySQL.
Upvotes: 1
Views: 3731
Reputation: 17640
I don't see why you are using a union (ie a vertical merge) when a join(a horizontal merge) would seem more appropriate
SELECT i.*,
p.id AS 'PartId',
m.`Name` AS 'Model',
c.`Name` AS 'Color',
pt.`Name` AS 'PartType'
FROM
inventory i
JOIN part p ON p.Id = i.PartId
JOIN model m ON m.Id = p.ModelId
JOIN color c ON c.Id = p.ColorId
JOIN parttype pt ON pt.Id = p.PartTypeId
WHERE
barcode1 = @Barcode
OR barcode2 = @Barcode
OR barcode3 = @Barcode
OR barcode4 = @Barcode
Upvotes: 0
Reputation: 1159
The problem you have is because you use Union
on two tables with different number of column. For Example if you get something in TableA:
| Id | Name | Age|
+----+------+----+
| 1 | Andy | 3 |
And another TableB
| Id | Name | Age| Gender |
+----+------+----+--------|
| 1 | Andy | 3 | Male |
If you try to union them you will get error, as they cannot be combined (because of different number of columns).
So your problem is that your first Select *...
return more columns than the second select where you select only 4 columns. The key to solve it, is to specify exact which columns you need to select so that both selects have same number of columns.
Something like this
SELECT
p.id AS 'PartId',
m.`Name` AS 'Model',
c.`Name` AS 'Color',
pt.`Name` AS 'PartType'
FROM
inventory i
JOIN part p ON p.Id = i.PartId
JOIN model m ON m.Id = p.ModelId
JOIN color c ON c.Id = p.ColorId
JOIN parttype pt ON pt.Id = p.PartTypeId
WHERE
i.barcode1 = 'DR963560860GYYQBKBBD2'
OR i.barcode2 = 'DR963560860GYYQBKBBD2'
OR i.barcode3 = 'DR963560860GYYQBKBBD2'
OR i.barcode4 = 'DR963560860GYYQBKBBD2'
UNION
SELECT
p.id AS 'PartId',
m.`Name` AS 'Model',
c.`Name` AS 'Color',
pt.`Name` AS 'PartType'
FROM
inventory i
JOIN part p ON p.Id = i.PartId
JOIN model m ON m.Id = p.ModelId
JOIN color c ON c.Id = p.ColorId
JOIN parttype pt ON pt.Id = p.PartTypeId
WHERE
barcode1 = @barcode
OR barcode2 = @barcode
OR barcode3 = 'DR963560860GYYQBKBBD2'
OR barcode4 = 'DR963560860GYYQBKBBD2';
Upvotes: 3