简体   繁体   English

SQL Server联接和通配符

[英]SQL Server join and wildcards

I want to get the results of a left join between two tables, with both having a column of the same name, the column on which I join. 我想获得两个表之间的左联接的结果,两个表都具有同名的列,即我联接的列。 The following query is seen as valid by the import/export wizard in SQL Server, but it always gives an error. 以下查询被SQL Server中的导入/导出向导视为有效,但始终会给出错误。 I have some more conditions, so the size wouldn't be too much. 我还有其他条件,因此大小不会太大。 We're using SQL Server 2000 iirc and since we're using an externally developed program to interact with the database (except for some information we can't retrieve that way), we can not simply change the column name. 我们使用的是SQL Server 2000 iirc,并且由于使用的是外部开发的程序来与数据库进行交互(除了一些无法以这种方式检索的信息),我们不能简单地更改列名。

SELECT table1.*, table2.* 
FROM table1 
LEFT JOIN table2 ON table1.samename = table2.samename

At least, I think the column name is the problem, or am I doing something else wrong? 至少,我认为列名有问题,还是我做错了其他事?

Do more columns than just your join key have the same name? 具有相同名称的不仅仅是您的联接键的列吗? If only your join key has the same name then simply select one of them since the values will be equivalent except for the non-matching rows (which will be NULL). 如果只有您的联接键具有相同的名称,则只需选择其中之一,因为除了不匹配的行(NULL)之外,这些值将是等效的。 You will have to enumerate all your other columns from one of the tables though. 不过,您将不得不枚举其中一个表中的所有其他列。

SELECT table2.samename,table1.othercolumns,table2.*
FROM table1 
LEFT JOIN table2 ON table1.samename = table2.samename

You may need to explicitly list the columns from one of the tables (the one with less fields), and leave out the 2nd instance of what would be the duplicate field.. 您可能需要显式列出其中一个表(具有较少字段的表)中的列,而忽略第二个重复字段的实例。

select Table1.*, {skip the field Table2.sameName} Table2.fld2, Table2.Fld3, Table2.Fld4...  from

Since its a common column, it APPEARS its trying to create twice in the result set, thus choking your process. 由于它是一个公共列,因此它似乎试图在结果集中创建两次,从而使您的过程停滞不前。

Since you should never use select *, simply replace it with the column names of the columns you want. 由于永远不要使用select *,只需将其替换为所需列的列名即可。 THe join column has the same value (or null) in both sides of the join, so only select one of themm the one from table1 which will always have the value. 连接的列在连接的两边都具有相同的值(或为null),因此请仅从表1中选择一个将始终具有该值的一个。

If you want to select all the columns from both tables just use Select * instead of including the tables separately. 如果要从两个表中选择所有列,请使用Select *而不是单独包括这些表。 That will however leave you with duplicate column names in the result set, so even reading them out by name will not work and reading them by index will give inconsistent results, as changing the columns in the database will change the resultset, breaking any code depending on the ordinals of the columns. 但是,这将使您在结果集中留有重复的列名,因此即使按名称读出它们也不起作用,而按索引读取它们也会产生不一致的结果,因为更改数据库中的列将更改结果集,从而破坏了任何代码在列的序号上。

Unfortunately the best solution is to specify exactly the columns you need and create aliases for the duplicates so they are unique. 不幸的是,最好的解决方案是准确地指定所需的列,并为重复项创建别名,使它们唯一。

I quickly get the column headings by setting the query to text mode and copying the top row ... 我通过将查询设置为文本模式并复制第一行来快速获得列标题。

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

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