Artikel Directory
MySQL databankUny folchoarder troch query syntaksis / útspraak gebrûk
MySQL UNION operator
Dizze tutorial lit jo de syntaksis en foarbylden fan 'e MySQL UNION-operator yntrodusearje.
beskriuwing
De MySQL UNION-operator wurdt brûkt om de resultaten fan twa of mear SELECT-útspraken te kombinearjen yn ien resultaatset.Meardere SELECT-útspraken ferwiderje dûbele gegevens.
grammatika
MySQL UNION operator syntaksis opmaak:
SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions] UNION [ALL | DISTINCT] SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions];
Parameter
- expression1, expression2, ... expression_n: Kolom te heljen.
- tabellen: De gegevenstabel om te heljen.
- WHERE betingsten: Opsjoneel, sykkritearia.
- DISTINCT: Ferwiderje opsjoneel dûbele gegevens út 'e resultaatset.De UNION-operator hat standert gegevens deduplisearre, sadat de DISTINCT-modifier gjin effekt hat op it resultaat.
- ALLE: Opsjoneel, jout alle resultaatsets werom, ynklusyf duplikaten.
demo databank
Yn dizze tutorial sille wy de chenweiliang-sampledatabase brûke.
Hjir binne de gegevens fan 'e 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 | +----+---------------+---------------------------+-------+---------+
Hjir binne de gegevens foar de "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 eksimplaar
De folgjende SQL-statement selekteart alles út 'e tabellen "Websites" en "apps".ferskillendlân (allinich ûnderskate wearden):
Instance
SELECT country FROM Websites UNION SELECT country FROM apps ORDER BY country;
SQL UNION ALL eksimplaar
De folgjende SQL-statement brûkt UNION ALL om te selektearjen út 'e tabellen "Websites" en "apps".allelân (hat ek dûbele wearden):
Instance
SELECT country FROM Websites UNION ALL SELECT country FROM apps ORDER BY country;
SQL UNION ALLE mei WHERE
De folgjende SQL-statement brûkt UNION ALL om te selektearjen út 'e tabellen "Websites" en "apps".alleGegevens foar Sina (CN) (ek mei dûbele wearden):
Instance
SELECT country, name FROM Websites WHERE country='CN' UNION ALL SELECT country, app_name FROM apps WHERE country='CN' ORDER BY country;
Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) dield "union folchoarder troch query syntaksis / ferklearring gebrûk yn MySQL databank", dat is nuttich foar jo.
Wolkom om de keppeling fan dit artikel te dielen:https://www.chenweiliang.com/cwl-475.html
Wolkom by it Telegram-kanaal fan Chen Weiliang's blog om de lêste updates te krijen!
📚 Dizze hantlieding befettet enoarme wearde, 🌟Dit is in seldsume kâns, mis it net! ⏰⌛💨
Diel en like as jo wolle!
Jo dielen en likes binne ús trochgeande motivaasje!