Uni urutan ku query sintaksis / pamakéan pernyataan dina database MySQL

Pamakéan sintaksis/pernyataan query dina database MySQL UNION ORDER BY

MySQL operator UNION

Tutorial ieu ngenalkeun anjeun kana sintaksis sareng conto operator MySQL UNION.

katerangan

Operator MySQL UNION dipaké pikeun ngagabungkeun hasil dua atawa leuwih pernyataan SELECT kana hiji set hasil tunggal.Sababaraha pernyataan SELECT miceun duplikat data.

tata basa

Format sintaksis operator MySQL UNION:

SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

parameter

  • babasan1, babasan2, ... babasan_n: Kolom pikeun dipulut.
  • tabél: Tabél data pikeun dipulut.
  • Syarat mana: Pilihan, kriteria pilarian.
  • Béda: Optionally, piceun duplikat data tina set hasil.Operator UNION gaduh data deduplicated sacara standar, ku kituna modifier DISTINCT teu aya pangaruhna kana hasilna.
  • KABEH: Opsional, mulihkeun sadaya set hasil, kalebet duplikat.

database demo

Dina tutorial ieu, urang bakal ngagunakeun database sampel chenweiliang.

Ieu data tina tabel "Situs Web":

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     |
+----+---------------+---------------------------+-------+---------+

Ieu data kanggo "aplikasi" 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 conto

Pernyataan SQL di handap ieu milih sadaya nagara anu béda (ngan ukur nilai anu béda) tina tabel "Websites" sareng "apps" :

Contona

SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country;
 
Catetan:UNION teu tiasa dianggo pikeun daptar sadaya nagara dina duanana tabel.Upami sababaraha situs wéb sareng aplikasi asalna ti nagara anu sami, unggal nagara ngan bakal didaptarkeun sakali. UNION ngan ukur milih nilai anu béda.Punten nganggo UNION ALL pikeun milih nilai duplikat!

SQL UNION ALL conto

Pernyataan SQL di handap ieu nganggo UNION ALL pikeun milih sadaya nagara (kalebet nilai duplikat) tina tabel "Websites" sareng "apps" :

Contona

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

 


SQL UNION ALL kalawan WHERE

Pernyataan SQL di handap ieu nganggo UNION ALL pikeun milih sadaya data pikeun Cina (CN) tina tabel "Website" sareng "apps" (kalebet nilai duplikat):

Contona

SELECT country, name FROM Websites
WHERE country='CN'
UNION ALL
SELECT country, app_name FROM apps
WHERE country='CN'
ORDER BY country;

koméntar

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

Ngagulung ka luhur