During our development process, we often use multiple query results to be displayed directly. So how should we query? This article is about learning SQL. Let’s take a look at the UNION keyword in SQL and how to use it. I hope it will be helpful to everyone!
#The UNION keyword in SQL will display the results of multiple query conditions.
The Chinese meaning of UNION is union, that is, merging the results of two or more SELECT statements. The usage tips are as follows:
As shown below, use the data introduced in the previous chapter as demonstration data
Create tables respectively s_user
and table s_user_1
CREATE TABLE `s_user` ( `userid` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(255) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `pass_word` varchar(255) DEFAULT NULL, `salt` varchar(255) DEFAULT NULL, `state` varchar(255) DEFAULT NULL, `create_time` datetime DEFAULT NULL, PRIMARY KEY (`userid`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
The test data is as follows:
INSERT INTO `test`.`s_user`(`userid`, `user_name`, `name`, `pass_word`, `salt`, `state`, `create_time`) VALUES (1, '小米', '小米', '123123', '123', '1', '2022-02-10 19:48:43'); INSERT INTO `test`.`s_user`(`userid`, `user_name`, `name`, `pass_word`, `salt`, `state`, `create_time`) VALUES (2, '小白', '小白', '123123', '123', '1', '2022-02-10 19:48:43'); INSERT INTO `test`.`s_user_1`(`userid`, `user_name`, `name`, `pass_word`, `salt`, `state`, `create_time`) VALUES (1, '小红', '小红', '123123', '123', '1', '2022-02-10 19:48:43'); INSERT INTO `test`.`s_user_1`(`userid`, `user_name`, `name`, `pass_word`, `salt`, `state`, `create_time`) VALUES (2, '小黄', '小黄', '123123', '123', '1', '2022-02-10 19:48:43');
Each SELECT statement inside UNION must have the same number of columns: query first SQL
SELECT * FROM `s_user` UNION SELECT * FROM `s_user_1`
with the same columns queries all columns. You can see that the execution results are as follows.
If the first query is for all columns and the second query is for three fields, what is the query result? Now execute the following SQL:
Error usage
SELECT * FROM `s_user` UNION SELECT userid,name,pass_word FROM `s_user_1`
##Error message: The SELECT statement used to query a different number of columns
Correct usage
SELECT userid,name,pass_word FROM `s_user` UNION SELECT userid,name,pass_word FROM `s_user_1`
SELECT userid,name,create_time FROM `s_user` UNION SELECT pass_word,name,userid FROM `s_user_1`
SELECT create_time,userid,name FROM `s_user` UNION SELECT pass_word,name,userid FROM `s_user_1`
INSERT INTO `test`.`s_user_1`(`userid`, `user_name`, `name`, `pass_word`, `salt`, `state`, `create_time`) VALUES (3, '小白', '小白', '123123', '123', '1', '2022-02-10 19:48:43');
SELECT user_name,name,pass_word,salt FROM `s_user` UNION SELECT user_name,name,pass_word,salt FROM `s_user_1`
##Execute again
SELECT user_name,name,pass_word,salt FROM `s_user` UNION ALL SELECT user_name,name,pass_word,salt FROM `s_user_1`
Execution results:
You can see that UNION association removes duplicate items, while UNION ALL queries all values without removing duplicate data.
Conclusion
Recommended learning:
mysql video tutorialThe above is the detailed content of SQL introductory learning: A brief analysis of the usage of the UNION keyword. For more information, please follow other related articles on the PHP Chinese website!