Uny folchoarder troch query syntaksis / statement gebrûk yn MySQL databank

MySQL-database UNION ORDER BY query syntaksis/statement 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-opdracht selektearret alle ûnderskate lannen (allinich ûnderskate wearden) út 'e tabellen "Websides" en "apps" :

Instance

SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country;
 
Oantekeningen:UNION kin net brûkt wurde om alle lannen yn beide tabellen te listjen.As guon websiden en apps út itselde lân komme, sil elk lân mar ien kear wurde fermeld. UNION kiest gewoan ûnderskate wearden.Brûk asjebleaft UNION ALL om dûbele wearden te selektearjen!

SQL UNION ALL eksimplaar

De folgjende SQL-opdracht brûkt UNION ALL om alle lannen (ynklusyf dûbele wearden) te selektearjen út 'e tabellen "Websides" en "apps" :

Instance

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

 


SQL UNION ALLE mei WHERE

De folgjende SQL-opdracht brûkt UNION ALL om alle gegevens foar Sina (CN) te selektearjen út 'e tabellen "Websides" en "apps" (ynklusyf 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;

发表 评论

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

Scroll nei boppen