柠檬工会_工会经营者
檸檬工會(huì)
Hey guys! This week we’ll be going over some ways to work with result sets in MySQL. These result sets are the outputs of your everyday queries, such as:
大家好! 本周,我們將介紹一些在MySQL中處理結(jié)果集的方法。 這些結(jié)果集是您日常查詢的輸出,例如:
聯(lián)盟 (UNION)
One way to combine two result sets is by using the UNION operator. By default, this combines two result sets without duplicating any rows. This works as you would believe it would in set theory. The syntax for this would be:
合并兩個(gè)結(jié)果集的一種方法是使用UNION運(yùn)算符。 默認(rèn)情況下,這將合并兩個(gè)結(jié)果集,而不會(huì)重復(fù)任何行。 正如您所相信的那樣,這會(huì)起作用。 語法如下:
Where you have your regular SELECT statements, but use UNION to combine each result set. Each UNION can be followed by either DISTINCT or ALL. DISTINCT will combine the sets but keep each unique value only once, while ALL will combine the sets while keeping duplicates. If you don’t specify either, DISTINCT is used, which is pretty intuitive.
在具有常規(guī)SELECT語句的位置,但使用UNION合并每個(gè)結(jié)果集。 每個(gè)UNION后面都可以加上DISTINCT或ALL 。 DISTINCT將合并集合,但每個(gè)唯一值僅保留一次,而ALL將合并集合,同時(shí)保留重復(fù)項(xiàng)。 如果您未指定任何一個(gè),則使用DISTINCT ,這非常直觀。
Do note that each SELECT statement should have the same number of columns selected and the data types of the columns should be compatible.
請(qǐng)注意,每個(gè)SELECT語句應(yīng)選擇相同數(shù)量的列,并且列的數(shù)據(jù)類型應(yīng)兼容。
Now let’s look at an example using this. Let’s say we have two tables, table1 and table2, as shown below:
現(xiàn)在讓我們來看一個(gè)使用此示例。 假設(shè)我們有兩個(gè)表,table1和table2,如下所示:
If we were to combine both columns, col1 and col2, from each table, by following the syntax:
如果我們要通過遵循以下語法將每個(gè)表中的列col1和col2組合在一起:
Will produce the following:
將產(chǎn)生以下內(nèi)容:
Also note that the result’s column names will match those of the first SELECT statement. We can also use aliasing to change this by aliasing the column names of the first SELECT statement.
還要注意,結(jié)果的列名將與第一個(gè)SELECT語句的列名匹配。 我們還可以使用別名來更改別名,方法是對(duì)第一個(gè)SELECT語句的列名進(jìn)行別名處理。
You may have noticed that UNION is similar to JOIN. They both merge your data. However, UNION combines sets vertically, while JOIN combines sets horizontally, as illustrated below:
您可能已經(jīng)注意到UNION與JOIN類似。 他們都合并您的數(shù)據(jù)。 但是,UNION垂直組合集合,而JOIN水平組合集合,如下所示:
Union vs. (Inner) Join聯(lián)合與(內(nèi)部)合并Now we know how to combine different result sets into a single one!
現(xiàn)在我們知道如何將不同的結(jié)果集組合為一個(gè)!
The following resource is a huge help for anything SQL related!
以下資源對(duì)于任何與SQL相關(guān)的問題都是巨大的幫助!
翻譯自: https://medium.com/@gl7526/the-union-operator-4721f2ca3482
檸檬工會(huì)
總結(jié)
以上是生活随笔為你收集整理的柠檬工会_工会经营者的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 周公解梦梦到自己流鼻血是什么意思
- 下一篇: 梦到蛇是怀孕的征兆吗