Union-volgorde op querysyntaxis/instructiegebruik in MySQL-database

Syntaxis/gebruik van de UNION ORDER BY-query in een MySQL-database

MySQL UNION-operator

In deze zelfstudie maakt u kennis met de syntaxis en voorbeelden van de MySQL UNION-operator.

Beschrijving

De MySQL UNION-operator wordt gebruikt om de resultaten van twee of meer SELECT-instructies te combineren in één resultaatset.Meerdere SELECT-instructies verwijderen dubbele gegevens.

Grammatica

Syntaxisindeling voor MySQL UNION-operator:

SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

参数

  • uitdrukking1, uitdrukking2, ... uitdrukking_n: Kolom om op te halen.
  • tafels: De gegevenstabel die moet worden opgehaald.
  • WAAR voorwaarden: Optioneel, zoekcriteria.
  • VERSCHILLEND: Verwijder eventueel dubbele gegevens uit de resultatenset.De UNION-operator heeft standaard gegevens gededupliceerd, dus de modifier DISTINCT heeft geen effect op het resultaat.
  • ALLE: Optioneel, retourneert alle resultaatsets, inclusief duplicaten.

demo-database

In deze zelfstudie gebruiken we de chenweiliang-voorbeelddatabase.

Dit zijn de gegevens uit de tabel "Websites":

mysql> SELECT * FROM Websites;
+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 陈沩亮博客      | http://www.chenweiliang.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
| 7  | stackoverflow | http://stackoverflow.com/ |   0 | IND     |
+----+---------------+---------------------------+-------+---------+

Hier zijn de gegevens voor de app "apps":

mysql> SELECT * FROM apps;
+----+------------+-------------------------+---------+
| id | app_name   | url                     | country |
+----+------------+-------------------------+---------+
|  1 | QQ APP     | http://im.qq.com/       | CN      |
|  2 | 微博 APP | http://weibo.com/       | CN      |
|  3 | 淘宝 APP | https://www.taobao.com/ | CN      |
+----+------------+-------------------------+---------+
3 rows in set (0.00 sec)

 


SQL UNION-instantie

De volgende SQL-instructie selecteert alle unieke landen (alleen unieke waarden) uit de tabellen "Websites" en "apps" :

Voorbeeld

SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country;
 
注释:UNION kan niet worden gebruikt om alle landen in beide tabellen weer te geven.Als sommige websites en apps uit hetzelfde land komen, wordt elk land slechts één keer vermeld. UNION kiest gewoon verschillende waarden.Gebruik UNION ALL om dubbele waarden te selecteren!

SQL UNION ALL-instantie

De volgende SQL-instructie gebruikt UNION ALL om alle landen (inclusief dubbele waarden) uit de tabellen "Websites" en "apps" te selecteren:

Voorbeeld

SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;

 


SQL UNION ALL met WHERE

De volgende SQL-instructie gebruikt UNION ALL om alle gegevens voor China (CN) uit de tabellen "Websites" en "apps" te selecteren (inclusief dubbele waarden):

Voorbeeld

SELECT country, name FROM Websites
WHERE country='CN'
UNION ALL
SELECT country, app_name FROM apps
WHERE country='CN'
ORDER BY country;

发表 评论

您的邮箱地址不会被公开。必填项已用*标注

Scroll naar boven