Artikelkatalog
MySQL-databas UNION ORDER BY frågesyntax/satsanvändning
MySQL UNION operatör
Denna handledning introducerar dig till syntaxen och exemplen på MySQL UNION-operatorn.
Beskrivning
MySQL UNION-operatorn används för att kombinera resultaten av två eller flera SELECT-satser till en enda resultatuppsättning.Flera SELECT-satser tar bort dubbletter av data.
grammatik
MySQL UNION operatorsyntaxformat:
SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions] UNION [ALL | DISTINCT] SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions];
参数
- expression1, expression2, ... expression_n: Kolumn att hämta.
- tabeller: Datatabellen att hämta.
- WHERE villkor: Valfritt, sökkriterier.
- DISTINKT: Om du vill kan du ta bort dubblettdata från resultatuppsättningen.UNION-operatorn har deduplicerad data som standard, så DISTINCT-modifieraren har ingen effekt på resultatet.
- ALLT: Valfritt, returnerar alla resultatuppsättningar, inklusive dubbletter.
demodatabas
I den här handledningen kommer vi att använda chenweiliang-exempeldatabasen.
Här är data från tabellen "Webbplatser":
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 | +----+---------------+---------------------------+-------+---------+
Här är data för "apps"-appen:
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-instans
Följande SQL-sats väljer alla distinkta länder (endast distinkta värden) från tabellerna "Webbplatser" och "appar" :
exempel
SELECT country FROM Websites UNION SELECT country FROM apps ORDER BY country;
SQL UNION ALL-instans
Följande SQL-sats använder UNION ALL för att välja alla länder (inklusive duplicerade värden) från tabellerna "Webbplatser" och "appar" :
exempel
SELECT country FROM Websites UNION ALL SELECT country FROM apps ORDER BY country;
SQL UNION ALLA med WHERE
Följande SQL-sats använder UNION ALL för att välja all data för Kina (CN) från tabellerna "Webbplatser" och "Appar" (inklusive duplicerade värden):
exempel
SELECT country, name FROM Websites WHERE country='CN' UNION ALL SELECT country, app_name FROM apps WHERE country='CN' ORDER BY country;
Förhoppningsvis kommer artikeln "Union Order By Query Syntax/Statement Usage in MySQL Database" som delas på Chen Weiliangs blogg ( https://www.chenweiliang.com/ ) att vara till hjälp för dig.
Dela gärna länken till den här artikeln: https://www.chenweiliang.com/cwl-475.html
