FROM bang
[WHERE dieu_kien]
UNION
SELECT bieu_thuc1, bieu_thuc2, … bieu_thucn
FROM bang
[WHERE
dieu_kien]; bieu_thuc1, bieu_thuc2, . bieu_thucn
The column or calculation value you want to retrieve.
state
Table wants to get the record. Must have at least 1 table in the FROM clause.
WHERE dieu_kien
Option. Conditions must satisfy for the selected record.
Note:
For example - return an information field
For example, the UNION operator returns a field from multiple SELECT statements (and fields with the same data type).
SELECT sanpham_id
FROM sanpham
UNION
SELECT sanpham_id
FROM hangtonkho;
In the above example, if any sanpham_id appears in both the sanpham and hangtonkho tables, only 1 sanpham_id will appear in the result set. If you do not want to remove the duplicate record, use the UNION ALL operator.
For example - use ORDER BY
The UNION operator can use the ORDER BY clause to sort query results.
SELECT danhba_id, danhba_ten
FROM danhba
WHERE ten_trang = 'QuanTriMang.com'
UNION
SELECT congty_id, congty_ten
FROM congty
WHERE ten_trang = 'TrangCuaBan.com'
ORDER B
Y 2;
In this example, because the column name in the two SELECT statements is different, it is easier to refer to the column in the ORDER BY statement by position in the result set. In the above example, we filter the results by namba_ten / congty_ten in ascending order, as stated by the phrase ORDER BY 2.
danhba_ten / congty_ten is in the second position in the result set.
Previous article: DELETE TOP command in SQL Server
The following article: UNION ALL operator in SQL Server