Welcome to MySQL Workbench:UNION操作符

Welcome to MySQL Workbench:UNION操作符_第1张图片

UNION操作符:

MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。

 SELECT test_NO1_author FROM test_no1
 UNION
 SELECT test_NO1_author FROM test_no2
 ORDER BY test_NO1_author; 
 
  SELECT test_NO1_author FROM test_no1
 UNION all
 SELECT test_NO1_author FROM test_no2
 ORDER BY test_NO1_author; 
CREATE TABLE test_NO1(
test_NO1_id INT NOT NULL AUTO_INCREMENT,
test_NO1_title VARCHAR(100) NOT NULL,
test_NO1_author VARCHAR(40) NOT NULL,
test_NO1_date DATE,
PRIMARY KEY ( test_NO1_id )
)ENGINE=InnoDB;

drop table test_no1;


INSERT INTO test_NO1 
    (test_NO1_title, test_NO1_author, test_NO1_date )
    VALUES
    ("programb", "welcome to programb", NOW());
    
    
select * from test_NO1;


INSERT INTO test_NO1 
    (test_NO1_title, test_NO1_author, test_NO1_date )
    VALUES
    ("programb2", "welcome to programb2", NOW());
    
INSERT INTO test_NO1 
    (test_NO1_title, test_NO1_author, test_NO1_date )
    VALUES
    ("programb3", "welcome to programb3", NOW());
    
    

SELECT * from test_NO1  WHERE test_NO1_title='programb2';

SELECT * from test_NO1;

update test_NO1 SET test_NO1_title='programb100' WHERE test_NO1_id=1;

SELECT * from test_NO1 where test_NO1_id=1;

SELECT * from test_NO1;

DELETE FROM test_NO1 WHERE test_NO1_id=1;

SELECT * from test_NO1;

 SELECT * from test_NO1  WHERE test_NO1_title LIKE '%programb2';
 
 INSERT INTO test_NO1 
    (test_NO1_title, test_NO1_author, test_NO1_date )
    VALUES
    ("programb2", "welcome.to.programb2", NOW());
 
 SELECT * from test_NO1  WHERE test_NO1_author LIKE '%programb2';
 
 SELECT * from test_NO1  WHERE test_NO1_author LIKE '%to%';
 
 SELECT * from test_NO1  WHERE test_NO1_author LIKE 'welcome%';
 
 SELECT * from test_NO1;
 
 CREATE TABLE test_NO2(
test_NO1_id INT NOT NULL AUTO_INCREMENT,
test_NO1_title VARCHAR(100) NOT NULL,
test_NO1_author VARCHAR(40) NOT NULL,
test_NO1_date DATE,
PRIMARY KEY ( test_NO1_id )
)ENGINE=InnoDB;

 INSERT INTO test_NO2
    (test_NO1_title, test_NO1_author, test_NO1_date )
    VALUES
    ("programb2", "welcome.to.programb2", NOW());
    
 SELECT * from test_NO2;
 
 SELECT test_NO1_author FROM test_no1
 UNION
 SELECT test_NO1_author FROM test_no2
 ORDER BY test_NO1_author; 
 
  SELECT test_NO1_author FROM test_no1
 UNION all
 SELECT test_NO1_author FROM test_no2
 ORDER BY test_NO1_author; 

Welcome to MySQL Workbench:UNION操作符_第2张图片
Welcome to MySQL Workbench:UNION操作符_第3张图片
Welcome to MySQL Workbench:UNION操作符_第4张图片
Welcome to MySQL Workbench:UNION操作符_第5张图片

你可能感兴趣的:(mysql,mysql)