Artikelgids
MySQL databasis UNION ORDER BY navraag sintaksis/stelling gebruik
MySQL UNIE operateur
Hierdie handleiding stel jou bekend aan die sintaksis en voorbeelde van die MySQL UNION-operateur.
beskrywing
Die MySQL UNION-operateur word gebruik om die resultate van twee of meer SELECT-stellings in 'n enkele resultaatstel te kombineer.Veelvuldige SELECT-stellings verwyder duplikaatdata.
grammatika
MySQL UNION operateur sintaksis formaat:
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 te herwin.
- tafels: Die datatabel om te herwin.
- WAAR voorwaardes: Opsioneel, soekkriteria.
- ONDERSKEI: Verwyder opsioneel duplikaatdata uit die resultaatstel.Die UNION-operateur het data by verstek gededupliseer, so die DISTINCT wysiger het geen effek op die resultaat nie.
- ALMAL: Opsioneel, gee alle resultaatstelle terug, insluitend duplikate.
demo databasis
In hierdie tutoriaal sal ons die chenweiliang-voorbeelddatabasis gebruik.
Hier is die data van die "Webwerwe"-tabel:
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 is die data vir die "apps" APP:
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 instansie
Die volgende SQL-stelling kies alle afsonderlike lande (slegs afsonderlike waardes) uit die "Webwerwe"- en "programme"-tabelle :
Instansie
SELECT country FROM Websites UNION SELECT country FROM apps ORDER BY country;
SQL UNION ALLE instansie
Die volgende SQL-stelling gebruik UNION ALL om alle lande (insluitend duplikaatwaardes) uit die "Webwerwe"- en "programme"-tabelle te kies:
Instansie
SELECT country FROM Websites UNION ALL SELECT country FROM apps ORDER BY country;
SQL UNION ALLES met WAAR
Die volgende SQL-stelling gebruik UNION ALL om alle data vir China (CN) uit die "Webwerwe"- en "programme"-tabelle (insluitend duplikaatwaardes) te kies:
Instansie
SELECT country, name FROM Websites WHERE country='CN' UNION ALL SELECT country, app_name FROM apps WHERE country='CN' ORDER BY country;
Hopelik sal die artikel "Union Order By Query Syntax/Statement Usage in MySQL Database" wat op Chen Weiliang se blog ( https://www.chenweiliang.com/ ) gedeel is, vir jou nuttig wees.
Deel gerus die skakel na hierdie artikel: https://www.chenweiliang.com/cwl-475.html
