![]() ![]() Syntax of UNION ALL mysql> SELECT column_name FROM table_a We use the UNION ALL when we want to get all the values and not the distinct values. In the employeeaddress table we can see repetition of city name like Bangalore, Chennai and Kolkata, state name like KA, TN and WB.īut using the UNION in the query returns the city and state name only once as it is selecting only the unique i.e., distinct values. mysql>SELECT city, state, country FROM employeeaddress In the following example we are listing all the city, state and country name that are distinct in both the employeeadress and office tables. When using UNION in our query we will get distinct values from the tables. ![]() | 6 | e05 | PRIMARY | Apartment 10 | Fast Food Block | Chennai | TN | INDIA | 600000 | | 5 | e04 | PRIMARY | Fruits Apartment | Fresh Block | Bangalore | KA | INDIA | 560000 | | 4 | e02 | PRIMARY | Pizza Street | Foodies Block | Kolkata | WB | INDIA | 700000 | | 3 | e03 | SECONDARY | House #301 ABC Colony | Jupiter Block | Kolkata | WB | INDIA | 700000 | | 2 | e03 | PRIMARY | Super Housing Complex Block 22 | Major Street | Chennai | TN | INDIA | 600000 | | 1 | e01 | PRIMARY | #12 street | Action block | Bangalore | KA | INDIA | 560000 | | id | employeeid | addresstype | addressline1 | addressline2 | city | state | country | pincode | Table: employeeaddress mysql> SELECT * FROM employeeaddress | o03 | Game | #12 Super Street | Master Block | Chennai | TN | INDIA | 600000 | ![]() | o02 | Apps | #321 Super Street | Action Road | Kolkata | WB | INDIA | 700000 | | o01 | Web Projects | #123 Awesome Street | Super Main | Bangalore | KA | INDIA | 560000 | | officeid | officename | addressline1 | addressline2 | city | state | country | pincode | Table: office mysql> SELECT * FROM office ![]() In this tutorial we will be using the office and employeeaddress table. Where, column_name must be of same type and same order in both the SELECT statements. Syntax of UNION mysql> SELECT column_name FROM table_a The data types of the columns in the SELECT query must also match.The columns in the SELECT query must also be in the same order.Each SELECT query must have the same number of columns when using UNION.We use the UNION operator when we want to combine the results of multiple SELECT query.įollowing are the points to note when using UNION in a query. In this tutorial we will learn about union and union all in MySQL. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |