Union-volgorde op querysyntaxis/instructiegebruik in MySQL-database

MySQL-databaseUnion-volgorde op querysyntaxis/instructiegebruik

MySQL UNION-operator

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

Omschrijving

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 alles uit de tabellen "Websites" en "apps".andersland (alleen verschillende waarden):

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 te selecteren uit de tabellen "Websites" en "apps".allemaalland (heeft ook dubbele waarden):

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 te selecteren uit de tabellen "Websites" en "apps".allemaalGegevens voor China (CN) (ook met 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;

Hoop Chen Weiliang Blog ( https://www.chenweiliang.com/ ) gedeelde "union order by query syntax/statement use in MySQL database", wat handig voor je is.

Welkom om de link van dit artikel te delen:https://www.chenweiliang.com/cwl-475.html

Welkom op het Telegram-kanaal van Chen Weiliang's blog voor de laatste updates!

🔔 Wees de eerste die de waardevolle "ChatGPT Content Marketing AI Tool Usage Guide" in de bovenste kanaaldirectory ontvangt! 🌟
📚 Deze gids bevat enorme waarde, 🌟Dit is een zeldzame kans, mis hem niet! ⏰⌛💨
Deel en like als je wilt!
Uw delen en likes zijn onze voortdurende motivatie!

 

发表 评论

Uw e-mailadres wordt niet gepubliceerd. 必填 项 已 用 * 标注

scroll naar boven