sql横着连接起来sql
SQL Join是什么意思? (What does a SQL Join mean?)
A SQL join describes the process of merging rows in two different tables or files together.
SQL连接描述了将两个不同表或文件中的行合并在一起的过程。
Rows of data are combined based on values in a selected column.
根据选定列中的值组合数据行。
In the example above, this is the Item column.
在上面的示例中,这是“ 项目”列。
An Item, Apple, has both a price and a quantity, but in different tables.
一个我TEM,苹果,既有价格和数量,但在不同的表。
Using a SQL join, these values become merged into one row!
使用SQL连接,这些值将合并为一行!
那么,为什么需要有关此内容的整篇文章? (So why do I need an entire article about this?)
In most cases, joining data won’t be as simple as the previous example. Oftentimes, we’ll have rows that can’t be joined because there isn’t the same value in the joining column for both tables.
在大多数情况下,合并数据不会像前面的示例那样简单。 通常,我们会有无法连接的行,因为两个表的连接列中的值都不相同。
For instance, what if there wasn’t a quantity for apple in the example above? How we handle what we do with rows like this depends on the type of SQL Join we choose.
例如,如果上面的示例中没有苹果数量,该怎么办? 处理此类行的方式取决于我们选择SQL Join的类型。
There are four main types of joins: inner join, left join, right join, and full join. In this article, I’ll explain to you what each type entails.
联接主要有四种类型:内部联接,左联接,右联接和完全联接。 在本文中,我将向您解释每种类型的含义。
Before going into everything, I think it’d be nice to mention that our tool Merge Spreadsheets is an easy to use tool that will easily perform all of this joining for you.
在介绍所有内容之前,我想很高兴地提到我们的工具“ 合并电子表格”是易于使用的工具,可以轻松地为您执行所有这些连接。
You can experiment with your spreadsheets to learn how these joins will look.
您可以尝试使用电子表格来了解这些联接的外观。
什么是内部联接? (What is an inner join?)
An inner join results in a table with rows where the values in the joining column are in both tables.
内部联接生成的表中具有行,其中联接列中的值都在两个表中。
To better understand this, let’s take a look at this example being joined on the item column:
为了更好地理解这一点,让我们看一下在item列上加入的示例:
Both of the starting tables have a lot of different items, but only Apple and Orange are in both. So, the final result will only include Apple and Orange with their price and quantity values merged into the same row.
两个起始表都有很多不同的项目,但是只有Apple和Orange都在。 因此,最终结果将仅包括将价格和数量值合并到同一行的Apple和Orange。
什么是左联接? (What is a left join?)
A left join will include all of the rows in the left table (file one), regardless of if they’re in the right table.
左联接将包括左表(文件一)中的所有行,无论它们是否在右表中。
Let’s take a look at the same tables as before, also being merged on the item column:
让我们看一下与以前相同的表,它们也被合并在item列上:
Like with inner join, the final product of this join includes Apple and Orange. However, it also includes all the other rows from file one, even if they don’t have quantity values (those cells will be left blank)!
与内部联接一样,此联接的最终产品包括Apple和Orange。 但是,它也包括文件一中的所有其他行,即使它们没有数量值(这些单元格将留为空白)!
什么是正确的联接? (What is a right join?)
A right join is the exact opposite of a left join; it includes all the values in the right table (file two) regardless of what’s in the first table.
右连接与左连接完全相反; 它包括右表(文件2)中的所有值,而不管第一个表中的内容如何。
So, in the same example as before, this will include Apple and Orange while also including any other rows in file two!
因此,在与之前相同的示例中,这将包括Apple和Orange,同时还包括文件2中的任何其他行!
什么是完全加入? (What is a full join?)
A full join will combine all the data from both spreadsheets while merging the rows that can be merged.
完全联接将合并两个电子表格中的所有数据,同时合并可以合并的行。
Full join is definitely the way to go if you don’t want to exclude any data in your final result.
如果您不想在最终结果中排除任何数据,完全连接绝对是正确的选择。
As always, using the same two tables as before will yield a different result when using a full join.
与往常一样,使用完全连接时,使用与以前相同的两个表将产生不同的结果。
All the rows from both tables are included and Apple and Orange become merged (as those are the only common items)!
两个表中的所有行都包括在内,Apple和Orange合并(因为这是唯一的常见项目)!
有趣的是,这会变得更加复杂吗? (Interesting, so does this get more complicated?)
It gets a little more complicated. There are a couple special cases that I’ll go into so that you’ll have a better understanding of how joining works!
它变得有点复杂。 我将介绍几种特殊情况,以便您对加入的工作方式有更好的了解!
如果我的数据重复了怎么办? (What if I have duplicates in my data?)
If you have duplicates in one table, they’ll also show up in your joined table depending on the join type.
如果您在一个表中有重复项,则它们还将显示在联接表中,具体取决于联接类型。
If there is a single row in one file with the same value in the joining column as multiple rows in the second file, those data in that single row will be repeated for each instance.
如果一个文件中的单行与第二个文件中的多行在连接列中具有相同的值,则将针对每个实例重复该单行中的那些数据。
For example, let’s look at the tables below being Inner joined on the Item column. In file one, there are two Orange rows while there’s only one in file two.
例如,让我们看一下在“ 项目”列上内部连接的下表。 在文件一中,有两个橙色行,而在文件二中只有一个。
Joining this will include both of the unique rows from file one while repeating the data for quantity from file two!
加入此文件将包括文件1的两个唯一行,同时重复文件2的数量数据!
By the way, a LEFT join would also produce the same result
顺便说一句, LEFT连接也会产生相同的结果
我可以共同加入多个列吗? (Can I join with multiple columns in common?)
Yes! If you want to join a table based on multiple columns, each grouping will be classified as its own unique entity.
是! 如果要基于多个列联接表,则每个分组都将分类为自己的唯一实体。
For instance, in the example below, we’ll be Full joining on the Item and Price columns.
例如,在下面的示例中,我们将在“ 项目”和“ 价格”列上进行“ 完全连接”。
The Apple row is the only one that’s merged because it’s the only one with the same Item and Price in both tables.
苹果行是唯一被合并的行,因为它是两个表中唯一具有相同项目和价格的行 。
Instead of both of the Orange rows being merged, they are treated as different rows because their price values are different.
由于它们的价格值不同,因此不会将两个橙色行都合并,而是将它们视为不同的行。
So, everything is the same as before but now we look for the same values in two columns rather than just one.
因此,一切都与以前相同,但是现在我们在两列中寻找相同的值,而不仅仅是一列。
如果我的公用栏中有空白值怎么办? (What if I have a blank value in my common column?)
Each blank value in the column you’re joining on will be treated like any normal value. In other words, you can join on a blank value as usual.
您要加入的列中的每个空白值都将被视为任何常规值。 换句话说,您可以照常加入空白值。
For example, in the tables above, there are blank values in both files in the item column. These will be treated as the same string and become joined! In the case of the example above, all types of joining will result in the same product.
例如,在上表中,项目列中的两个文件都有空白值。 这些将被视为相同的字符串并加入! 在上面的示例中,所有类型的连接都将产生相同的产品。
NOTE: While a blank value will be treated as any other string, a NULL value is different. NULL values denote nothing and are typically used in code. Each NULL value is treated as a unique entity and can’t be joined on. For more information, check out this article.
Let’s also take a look at a more comprehensive example with all of these special cases in it.
让我们也看看其中包含所有这些特殊情况的更全面的示例。
We will be performing a Left join on the Item and Price columns.
我们将在项目和价格列上执行左连接。
When performing a left join, all the rows from the left file will be in the final product.
执行左联接时,左文件中的所有行都将在最终产品中。
The Apple,$1 row is in both files, so the ID# and quantity for them will be merge.
Apple,$ 1行在两个文件中,因此它们的ID#和数量将合并。
Orange,$2 is only in the left file, so its quantity will remain blank.
橙色,$ 2仅在左侧文件中,因此其数量将保持空白。
Finally, we have a blank/blank row in the left file and two blank/blank rows in the second file. This means the the ID # for blank/blank in file one will be repeated twice to go with each quantity value in the second table!
最后,左侧文件中有一个空白行,第二个文件中有两个空白行。 这意味着文件1中空白/空白的ID#将重复两次,以与第二张表中的每个数量值对应!
And the fully blank row at the bottom will be ignored!
底部的空白行将被忽略!
所以...我们涵盖了所有内容吗? (So… have we covered everything?)
Yep! We just went over all the basics of SQL joins so you should be able to join tables with no problems now.
是的 我们仅介绍了SQL连接的所有基础知识,因此您现在应该可以毫无问题地连接表。
Feel free to experiment with your data in spreadsheets at Merge Spreadsheets because there is nothing like trying this out to really understand it.
可以在Merge Spreadsheets的电子表格中随意尝试数据,因为没有什么可以像尝试这种方法那样真正理解它了。
If you still have questions, don’t hesitate to reach out to info@lovespreadsheets.com!
如果您仍有疑问,请随时联系info@lovespreadsheets.com!
Want to learn more about how to Merge Spreadsheets? Check out our ultimate guide here!
想更多地了解如何合并电子表格? 在这里查看我们的最终指南!
翻译自: https://medium.com/@lovespreadsheets/a-brief-yet-comprehensive-introduction-to-sql-joins-de2fa412d2e
sql横着连接起来sql
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388429.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!