简体   繁体   English

使用的选择语句具有不同数量的mysql列

[英]the used select statements have a different number of columns mysql

I have an error "the used select statements have a different number of columns mysql" 我有一个错误“所使用的选择语句具有不同数量的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. 我要获取的是第一个select语句中库存表中的所有数据,另一个是来自另一个表。

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. 我是MySQL的初学者。

The problem you have is because you use Union on two tables with different number of column. 您遇到的问题是因为您在具有不同列数的两个表上使用了Union For Example if you get something in TableA: 例如,如果您在TableA中得到了一些东西:

| Id | Name | Age|
+----+------+----+
| 1  | Andy | 3  |

And another TableB 还有另一个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. 因此,您的问题是您的第一个Select *...返回的列比第二个select中仅选择4列的列多。 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';

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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