简体   繁体   English

使用的SELECT语句具有不同的列数

[英]The used SELECT statements have a different number of columns

For examples I don't know how many rows in each table are and I try to do like this: 例如,我不知道每个表中有多少行,我尝试这样做:

SELECT * FROM members 
UNION 
SELECT * FROM inventory

What can I put to the second SELECT instead of * to remove this error without adding NULL 's? 如果不添加NULL我可以将第二个SELECT替换为*以删除此错误?

Put the columns names explicitly rather than *, and make sure the number of columns and data types match for the same column in each select. 明确列出列名而不是*,并确保列和数据类型的数量与每个选择中的同一列匹配。

Update: 更新:

I really don't think you want to be UNIONing those tables, based on the tables names. 我真的不认为你想根据表名来联合这些表。 They don't seem to contain related data. 它们似乎不包含相关数据。 If you post your schema and describe what you are trying to achieve it is likely we can provide better help. 如果您发布架构并描述您要实现的目标,我们可能会提供更好的帮助。

you could do 你能做到的

SELECT *
from members
UNION
SELECT inventory.*, 'dummy1' AS membersCol1, 'dummy2' AS membersCol2
from inventory;

Where membersCol1 , membersCol12 , etc... are the names of columns from members that are not in inventory . 其中membersCol1membersCol12等等是来自不在inventory members的列的名称。 That way both queries in the union will have the same columns (Assuming that all the columns in inventory are the same as in members which seems very strange to me... but hey, it's your schema). 这样,联合中的两个查询都将具有相同的列(假设inventory中的所有列都与members中的相同,这对我来说似乎很奇怪......但是,嘿,这是您的架构)。

UPDATE : 更新

As HLGEM pointed out, this will only work if inventory has columns with the same names as members , and in the same order. 正如HLGEM指出的那样, 只有inventory具有与members具有相同名称的列并且按相同顺序时,这才有效。 Naming all the columns explicitly is the best idea, but since I don't know the names I can't exactly do that. 明确命名所有列是最好的想法,但由于我不知道名称,我不能完全这样做。 If I did, it might look something like this: 如果我这样做,它可能看起来像这样:

SELECT id, name, member_role, member_type
from members
UNION
SELECT id, name, '(dummy for union)' AS member_role, '(dummy for union)' AS member_type
from inventory;

I don't like using NULL for dummy values because then it's not always clear which part of the union a record came from - using 'dummy' makes it clear that the record is from the part of the union that didn't have that record (though sometimes this might not matter). 我不喜欢对虚拟值使用NULL,因为它并不总是清楚记录来自哪个联合部分 - 使用'dummy'可以清楚地表明记录来自没有该记录的联合部分(虽然有时这可能无关紧要)。 The very idea of unioning these two tables seems very strange to me because I very much doubt they'd have more than 1 or 2 columns with the same name, but you asked the question in such a way that I imagine in your scenario this somehow makes sense. 合并这两个表的想法对我来说似乎很奇怪,因为我非常怀疑他们有超过1或2列具有相同的名称,但是你以这样的方式提出问题我想象你的方案中这个某种方式说得通。

Are you sure you don't want a join instead? 你确定你不想加入吗? It is unlikely that UNOIN will give you what you want given the table names. 鉴于表名,UNOIN不太可能给你你想要的东西。

I don't know how many rows in each table 我不知道每个表中有多少行

Are you sure this isn't what you want? 你确定这不是你想要的吗?

SELECT 'members' AS TableName, Count(*) AS Cnt FROM members 
UNION ALL
SELECT 'inventory', Count(*) FROM inventory

MySQL UNION ALL运算符中的每个SELECT语句必须在具有相似数据类型的结果集中具有相同数量的字段访问https://www.techonthenet.com/mysql/union_all.php

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

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