Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; WP_MatchesMapRegex has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/class-wp.php on line 632

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Translation_Entry has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/entry.php on line 14

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_Reader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 12

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_FileReader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 120

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_StringReader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 175

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_CachedFileReader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 221

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_CachedIntFileReader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 236

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; WP_Widget_Factory has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/widgets.php on line 544

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzAdSense has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/easy-adsense-lite.php on line 38

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzAdsWidget has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/easy-adsense-lite.php on line 941

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzAdsSearch has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/easy-adsense-lite.php on line 1003

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzAdsLU has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/easy-adsense-lite.php on line 1078

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzBaseOption has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 22

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzCheckBox has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 174

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzRadioBox has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 199

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzSelect has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 250

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzMessage has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 294

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzHelpTag has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 313

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzHelpPopUp has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 328

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzTextArea has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 346

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzText has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 366

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzSubmit has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 385

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzColorPicker has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 409

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzOneTab has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 432

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzMiniTab has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 476
Union trong sql

Union trong sql

 

1.Câu lệnh UNION

Câu lệnh UNION được sử dụng để select (lựa chọn) những thông tin liên quan đến nhau của hai tables, số lượng những câu lệnh JOIN. Tuy nhiên nếu sử dụng câu lện UNION thì tất cả những sự lựa chọn tại các cột cần phải giống nhau về mặt dữ liệu.

Chú ý: Trong UNION những giá trị giống nhau sẽ là duy nhất (Coi như có câu lệnh DISTINCT ) .

Cú pháp:

SELECT column1 [, column2 ]FROM table1 [, table2 ]

[WHERE condition]

UNION

SELECT column1 [, column2 ]

FROM table1 [, table2 ]

[WHERE condition]

Ví dụ: có bảng customer:

+—-+———-+—–+———–+———-+

| ID | NAME     | AGE | ADDRESS   | SALARY  |

+—-+———-+—–+———–+———-+

|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |

|  2 | Khilan   |  25 | Delhi     |  1500.00 |

|  3 | kaushik  |  23 | Kota      |  2000.00 |

|  4 | Chaitali |  25 | Mumbai    |  6500.00 |

|  5 | Hardik   |  27 | Bhopal    |  8500.00 |

|  6 | Komal    |  22 | MP        |  4500.00 |

|  7 | Muffy    |  24 | Indore    | 10000.00 |

+—-+———-+—–+———–+———-+

 

 

 

Và bảng orders:

+—-+———————+————-+——–+

|OID  | DATE         | CUSTOMER_ID | AMOUNT |

+—–+———————+————-+——–+

| 102 | 2009-10-08 00:00:00 |           3 |   3000 |

| 100 | 2009-10-08 00:00:00 |           3 |   1500 |

| 101 | 2009-11-20 00:00:00 |           2 |   1560 |

| 103 | 2008-05-20 00:00:00 |           4 |   2060 |

+—–+———————+————-+——–+

Ta sẽ dùng câu lệnh:
SQL> SELECT  ID, NAME, AMOUNT, DATE

FROM CUSTOMERS

LEFT JOIN ORDERS

ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID

UNION

SELECT  ID, NAME, AMOUNT, DATE

FROM CUSTOMERS

RIGHT JOIN ORDERS

ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

 

 

 

 

 

Và được kết quả:

+——+———-+——–+———————+

| ID   | NAME     | AMOUNT | DATE                |

+——+———-+——–+———————+

|    1 | Ramesh   |   NULL | NULL                |

|    2 | Khilan   |   1560 | 2009-11-20 00:00:00 |

|    3 | kaushik  |   3000 | 2009-10-08 00:00:00 |

|    3 | kaushik  |   1500 | 2009-10-08 00:00:00 |

|    4 | Chaitali |   2060 | 2008-05-20 00:00:00 |

|    5 | Hardik   |   NULL | NULL                |

|    6 | Komal    |   NULL | NULL                |

|    7 | Muffy    |   NULL | NULL                |

+——+———-+——–+———————+

  1. Câu lệnh UNION ALL:

UNION ALL cú pháp tương tự UNION nhưng có điều nó sẽ list ra toàn bộ giá trị của cả 2 bảng.

Với 2 bảng ở trên,dùng câu lệnh:

SQL> SELECT  ID, NAME, AMOUNT, DATE

FROM CUSTOMERS

LEFT JOIN ORDERS

ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID

UNION ALL

SELECT  ID, NAME, AMOUNT, DATE

FROM CUSTOMERS

RIGHT JOIN ORDERS

ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

 

 

 

Ta sẽ được kết quả:

+——+———-+——–+———————+

| ID   | NAME     | AMOUNT | DATE                |

+——+———-+——–+———————+

|    1 | Ramesh   |   NULL | NULL                |

|    2 | Khilan   |   1560 | 2009-11-20 00:00:00 |

|    3 | kaushik  |   3000 | 2009-10-08 00:00:00 |

|    3 | kaushik  |   1500 | 2009-10-08 00:00:00 |

|    4 | Chaitali |   2060 | 2008-05-20 00:00:00 |

|    5 | Hardik   |   NULL | NULL                |

|    6 | Komal    |   NULL | NULL                |

|    7 | Muffy    |   NULL | NULL                |

|    3 | kaushik  |   3000 | 2009-10-08 00:00:00 |

|    3 | kaushik  |   1500 | 2009-10-08 00:00:00 |

|    2 | Khilan   |   1560 | 2009-11-20 00:00:00 |

|    4 | Chaitali |   2060 | 2008-05-20 00:00:00 |

+——+———-+——–+———————+

 

 

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">