简体   繁体   English

从具有相同列名的两个表中选择* mySQL

[英]select * from two tables with same column names mySQL

Let us say I have two tables with many columns so I do not want to name the column names explicitly in my query but i want to avoid duplicate names. 让我们说我有两个带有很多列的表,所以我不想在查询中显式地命名列名,但是我想避免重复的名字。

If I do: 如果我做:

  CREATE TABLE new_table
    SELECT a.*, b.*
    FROM  table1 a
    INNER JOIN table2 b ON a.myID = b.myId
    WHERE a.age > 10 and b.ice = 'melted'

I will get an error saying: duplicate column name myId , I could also get more errors if more column names in a and b are the same. 我会收到一条错误消息: duplicate column name myId ,如果a和b中的更多列名相同,我也会遇到更多错误。

How can I avoid this issue by automatically adding a prefix to all column names in a.* and b.* w/o explicitly mentioning all the column names - very tedious to do so! 我如何通过自动在a。*和b。*中的所有列名上添加前缀而不显式提及所有列名来避免此问题-这样做很繁琐!

Thanks! 谢谢!

Unfortunately, you will have to list the columns in case table have matching column names. 不幸的是,如果表具有匹配的列名,则必须列出这些列。 However, you can use information_schema to get the column names, format those and copy paste in the query to save the pain, eg: 但是,您可以使用information_schema来获取列名,设置其格式并在查询中复制粘贴以节省痛苦,例如:

SELECT GROUP_CONCAT(CONCAT('a.', COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = 'schema' AND TABLE_NAME = 'table';

The above query should give you comma separated column names with a. 上面的查询应为您提供以逗号分隔的列名a. prefix. 字首。 You can then use the same query for table b, get the names out and use it in the main SELECT query. 然后,您可以对表b使用相同的查询,获取名称,然后在主SELECT查询中使用它。

Update 更新

As @Uueerdo has rightly said, you can add alias to columns as well, eg: 正如@Uueerdo正确说的,您也可以为列添加别名,例如:

SELECT GROUP_CONCAT(CONCAT('a.', COLUMN_NAME, ' AS a_', COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = 'schema' AND TABLE_NAME = 'table';

In my experience ORMs will run an initial DESCRIBE query so it can do this sort of stuff for you once it has the column names. 以我的经验,ORM将运行一个初始的DESCRIBE查询,这样一旦具有列名,它就可以为您完成这种事情。 But if you insist on doing it dynamically in a single query, you could do this with pure MySQL: 但是,如果您坚持要在单个查询中动态执行此操作,则可以使用纯MySQL执行此操作:

-- config
SET @database = 'your_database';
SET @tableA = 'table1';
SET @tableB = 'table2';

-- table alias "a" columns
SET @fieldsA = NULL;
SELECT GROUP_CONCAT(CONCAT('a.', COLUMN_NAME), ' AS ',CONCAT('`a.', COLUMN_NAME,'`')) INTO @fieldsA
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = @database AND TABLE_NAME = @tableA;

-- table alias "b" columns
SET @fieldsB = NULL;
SELECT GROUP_CONCAT(CONCAT('b.', COLUMN_NAME), ' AS ',CONCAT('`b.', COLUMN_NAME,'`')) INTO @fieldsB
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = @database AND TABLE_NAME = @tableB;

-- some variables for readability
SET @fields = CONCAT(' ', @fieldsA, ',', @fieldsB,' ');
SET @tableAliasA = CONCAT(' ',@database, '.', @tableA,' a ');
SET @tableAliasB = CONCAT(' ',@database, '.', @tableB,' b ');


-- generate our final query
SET @query = CONCAT('CREATE TABLE new_table SELECT', @fields,
                     'FROM', @tableAliasA,
                     'INNER JOIN', @tableAliasB,
                     'ON a.myID = b.myId WHERE a.age > 10 and b.ice = ''melted''');

-- finally run the query:
PREPARE stmt1 FROM @query;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

-- if you have problems with the above query, uncomment the following to get the query so you can run it separately
-- SELECT @query;

I'd strongly advise against using this sort of solution though. 我强烈建议您不要使用这种解决方案。 I'd sooner run an initial DESCRIBE query as earlier stated, then generate your query based on that. 如前所述,我将尽快运行初始DESCRIBE查询,然后根据该查询生成您的查询。 Another solution is to create a temporary table as a copy of the second table, then rename problematic columns, then proceed to join on it to produce the data you need to create your new_table. 另一种解决方案是创建一个临时表作为第二个表的副本,然后重命名有问题的列,然后继续对其进行连接以生成创建new_table所需的数据。 MySQL has no issues with result columns having the same name, the issue here is trying to create a table with two columns with the same name. MySQL的结果列具有相同的名称没有问题,这里的问题是试图创建一个具有两个名称相同的列的表。 So essentially what you're trying to do is a star select but excluding a column. 因此,实际上,您要尝试的是星级选择,但排除一列。

Another approach is to just select only the primary key from both: 另一种方法是仅从两者中选择主键:
SELECT a.myID as `aId`, b.myId as `bId` then create your table containing only that. SELECT a.myID as `aId`, b.myId as `bId`然后创建仅包含该表的表。 Then if you ever need data from a particular table, just LEFT JOIN on it to grab the information you're looking for. 然后,如果您需要特定表中的数据,只需在其上左移即可获取您要查找的信息。 You can take this a step further and set up a VIEW to do this sort of thing for you. 您可以更进一步,并设置一个VIEW来为您做这种事情。 VIEWs can join tables for you and make it very easy to select whatever columns you're looking for. VIEW可以为您联接表,并可以很容易地选择要查找的列。 You can also setup multiple VIEWs as well. 您还可以设置多个VIEW。 Also note that views behave just like tables for the purpose of joins. 还要注意,出于联接的目的,视图的行为就像表一样。 You can JOIN a view with a table, or you can join a view with a view, etc. 您可以将视图与表联接,也可以将视图与视图联接,等等。

So rather than do what you're trying to do -- creating a new table with the data from two other tables -- consider whether you're actually looking for a VIEW . 因此,与其做您要尝试做的事情-用来自其他两个表的数据创建一个新表-还要考虑您是否真正在寻找VIEW

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

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