mysql union
创建一个测试表:
CREATE TABLE `user` (
`id` varchar(32) NOT NULL,
`name` varchar(10) DEFAULT NULL,
`age` int(10) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入一些测试数据:
INSERT INTO `_ceshi`.`user`(`id`, `name`, `age`, `address`) VALUES ('aaa', 'zhangsan', 12, '这里了');
INSERT INTO `_ceshi`.`user`(`id`, `name`, `age`, `address`) VALUES ('bbb', 'lisi', 13, '这里了');
INSERT INTO `_ceshi`.`user`(`id`, `name`, `age`, `address`) VALUES ('ccc', '王五', 14, '这里了');
INSERT INTO `_ceshi`.`user`(`id`, `name`, `age`, `address`) VALUES ('ddd', '赵六', 15, '这里了啊');
使用union查询:
-- union 会去除重复记录;union all 不会,是获取所有的
select count(*) count from
(
select * from user where address='这里了'
union
select * from user where address='这里了啊'
-- limit 1, 3
) t
;
结果: