UNION DISTINCT

Posted rsapaper

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UNION DISTINCT相关的知识,希望对你有一定的参考价值。

w同结构表读写合并。

 

  1 DROP PROCEDURE IF EXISTS w_ww_amzasin;
  2 DELIMITER /w/
  3 CREATE PROCEDURE w_ww_amzasin()
  4 BEGIN
  5   DECLARE done INT DEFAULT FALSE;
  6   DECLARE w_asin VARCHAR(16);
  7   DECLARE w_country VARCHAR(6);
  8   DECLARE wcur CURSOR FOR
  9                         SELECT asin,country FROM grab_amzreviews_asins
 10                                 UNION DISTINCT
 11                         SELECT asin,country FROM grab_amzsimilar_seller_asins
 12                                 UNION DISTINCT
 13                         SELECT asin,country FROM grab_amzlisting
 14                                 WHERE CONCAT(asin,LOWER(country)) NOT IN
 15                                     (SELECT CONCAT(asin,LOWER(country)) FROM amzasin) ;
 16   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
 17 
 18   OPEN wcur;
 19   read_loop: LOOP
 20     FETCH wcur INTO w_asin,w_country;
 21     IF done THEN LEAVE read_loop;
 22     END IF;
 23         SET @w_nothhtps=www.amazon.;  
 24         SET @w_most=CONCAT(LOWER(w_country),/product/dp/,w_asin);
 25         CASE  LOWER(w_country)
 26                 WHEN ca THEN SELECT w;
 27                 WHEN in THEN SELECT w;
 28                 WHEN es THEN SELECT w;
 29                 WHEN it THEN SELECT w;
 30                 WHEN fr THEN SELECT w;
 31 
 32                 WHEN jp THEN SET @w_most=CONCAT(co.,@w_most);
 33                 WHEN uk THEN SET @w_most=CONCAT(co.,@w_most);
 34 
 35                 WHEN mx THEN SET @w_most=CONCAT(com.,@w_most);
 36 
 37                 WHEN us THEN SET @w_most=CONCAT(com/gp/product/,w_asin);
 38       ELSE
 39         BEGIN
 40         END;
 41     END CASE;
 42 
 43         SET @w_nothhtps=CONCAT(@w_nothhtps,@w_most);
 44         SET @wtmp = CONCAT(INSERT INTO  amzasin (asin,country,url) VALUES ( ",w_asin,",",w_country,",",@w_nothhtps,"));
 45         PREPARE  stmt FROM @wtmp ;
 46         EXECUTE  stmt ;
 47         DROP PREPARE stmt;
 48   END LOOP;
 49   CLOSE wcur;
 50 END/w/
 51 DELIMITER;
 52 CALL w_ww_amzasin( );
 53 
 54 
 55 
 56 DROP PROCEDURE IF EXISTS w_ww_amzasin;
 57 DELIMITER /w/
 58 CREATE PROCEDURE w_ww_amzasin()
 59 BEGIN
 60   DECLARE done INT DEFAULT FALSE;
 61   DECLARE w_asin VARCHAR(16);
 62   DECLARE w_country VARCHAR(6);
 63   DECLARE wcur CURSOR FOR SELECT asin,country FROM grab_amzlisting WHERE CONCAT(asin,LOWER(country)) NOT IN (SELECT CONCAT(asin,LOWER(country)) FROM amzasin );
 64   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
 65 
 66   OPEN wcur;
 67   read_loop: LOOP
 68     FETCH wcur INTO w_asin,w_country;
 69     IF done THEN LEAVE read_loop;
 70     END IF;
 71         SET @w_nothhtps=www.amazon.;  
 72         SET @w_most=CONCAT(LOWER(w_country),/product/dp/,w_asin);
 73         CASE  LOWER(w_country)
 74                 WHEN ca THEN SELECT w;
 75                 WHEN in THEN SELECT w;
 76                 WHEN es THEN SELECT w;
 77                 WHEN it THEN SELECT w;
 78                 WHEN fr THEN SELECT w;
 79 
 80                 WHEN jp THEN SET @w_most=CONCAT(co.,@w_most);
 81                 WHEN uk THEN SET @w_most=CONCAT(co.,@w_most);
 82 
 83                 WHEN mx THEN SET @w_most=CONCAT(com.,@w_most);
 84 
 85                 WHEN us THEN SET @w_most=CONCAT(com/gp/product/,w_asin);
 86       ELSE
 87         BEGIN
 88         END;
 89     END CASE;
 90 
 91         SET @w_nothhtps=CONCAT(@w_nothhtps,@w_most);
 92         SET @wtmp = CONCAT(INSERT INTO  amzasin (asin,country,url) VALUES ( ",w_asin,",",w_country,",",@w_nothhtps,"));
 93         PREPARE  stmt FROM @wtmp ;
 94         EXECUTE  stmt ;
 95         DROP PREPARE stmt;
 96   END LOOP;
 97   CLOSE wcur;
 98 END/w/
 99 DELIMITER;
100 CALL w_ww_amzasin( );
101 
102 DROP PROCEDURE IF EXISTS w_ww_amzasin;
103 DELIMITER /w/
104 CREATE PROCEDURE w_ww_amzasin()
105 BEGIN
106   DECLARE done INT DEFAULT FALSE;
107   DECLARE w_asin VARCHAR(16);
108   DECLARE w_country VARCHAR(6);
109   DECLARE wcur CURSOR FOR SELECT asin,country FROM grab_amzsimilar_seller_asins WHERE CONCAT(asin,LOWER(country)) NOT IN (SELECT CONCAT(asin,LOWER(country)) FROM amzasin );
110   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
111 
112   OPEN wcur;
113   read_loop: LOOP
114     FETCH wcur INTO w_asin,w_country;
115     IF done THEN LEAVE read_loop;
116     END IF;
117         SET @w_nothhtps=www.amazon.;  
118         SET @w_most=CONCAT(LOWER(w_country),/product/dp/,w_asin);
119         CASE  LOWER(w_country)
120                 WHEN ca THEN SELECT w;
121                 WHEN in THEN SELECT w;
122                 WHEN es THEN SELECT w;
123                 WHEN it THEN SELECT w;
124                 WHEN fr THEN SELECT w;
125 
126                 WHEN jp THEN SET @w_most=CONCAT(co.,@w_most);
127                 WHEN uk THEN SET @w_most=CONCAT(co.,@w_most);
128 
129                 WHEN mx THEN SET @w_most=CONCAT(com.,@w_most);
130 
131                 WHEN us THEN SET @w_most=CONCAT(com/gp/product/,w_asin);
132       ELSE
133         BEGIN
134         END;
135     END CASE;
136 
137         SET @w_nothhtps=CONCAT(@w_nothhtps,@w_most);
138         SET @wtmp = CONCAT(INSERT INTO  amzasin (asin,country,url) VALUES ( ",w_asin,",",w_country,",",@w_nothhtps,"));
139         PREPARE  stmt FROM @wtmp ;
140         EXECUTE  stmt ;
141         DROP PREPARE stmt;
142   END LOOP;
143   CLOSE wcur;
144 END/w/
145 DELIMITER;
146 CALL w_ww_amzasin( );
147 
148 DROP PROCEDURE IF EXISTS w_ww_amzasin;
149 DELIMITER /w/
150 CREATE PROCEDURE w_ww_amzasin()
151 BEGIN
152   DECLARE done INT DEFAULT FALSE;
153   DECLARE w_asin VARCHAR(16);
154   DECLARE w_country VARCHAR(6);
155   DECLARE wcur CURSOR FOR SELECT asin,country FROM grab_amzreviews_asins WHERE CONCAT(asin,LOWER(country)) NOT IN (SELECT CONCAT(asin,LOWER(country)) FROM amzasin );
156   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
157 
158   OPEN wcur;
159   read_loop: LOOP
160     FETCH wcur INTO w_asin,w_country;
161     IF done THEN LEAVE read_loop;
162     END IF;
163         SET @w_nothhtps=www.amazon.;  
164         SET @w_most=CONCAT(LOWER(w_country),/product/dp/,w_asin);
165         CASE  LOWER(w_country)
166                 WHEN ca THEN SELECT w;
167                 WHEN in THEN SELECT w;
168                 WHEN es THEN SELECT w;
169                 WHEN it THEN SELECT w;
170                 WHEN fr THEN SELECT w;
171 
172                 WHEN jp THEN SET @w_most=CONCAT(co.,@w_most);
173                 WHEN uk THEN SET @w_most=CONCAT(co.,@w_most);
174 
175                 WHEN mx THEN SET @w_most=CONCAT(com.,@w_most);
176 
177                 WHEN us THEN SET @w_most=CONCAT(com/gp/product/,w_asin);
178       ELSE
179         BEGIN
180         END;
181     END CASE;
182 
183         SET @w_nothhtps=CONCAT(@w_nothhtps,@w_most);
184         SET @wtmp = CONCAT(INSERT INTO  amzasin (asin,country,url) VALUES ( ",w_asin,",",w_country,",",@w_nothhtps,"));
185         PREPARE  stmt FROM @wtmp ;
186         EXECUTE  stmt ;
187         DROP PREPARE stmt;
188   END LOOP;
189   CLOSE wcur;
190 END/w/
191 DELIMITER;
192 CALL w_ww_amzasin( );
193 
194 
195 
196 
197  
198  

 

以上是关于UNION DISTINCT的主要内容,如果未能解决你的问题,请参考以下文章

sql中,只使用union和先union all再distinct,两种方式哪个效率高?

MYSQL UNION DISTINCT

UNION DISTINCT

SQL Server 查询:Union vs Distinct union 所有性能

Postgres distinct union 仅适用于特定列

MySQL JOIN / UNION DISTINCT 问题