1

我有查询从两个表中选择一些列,这些列必须包含一些实际上是空的单元格,但必须在查询中命名它,例如

select name, REPLACE(format, ' – ', '/') as format, CAST(price AS UNSIGNED INTEGER) as price, CONCAT('http://example.com/',url_key) as url, level_id_value, delivery, sku from t1 where delivery <> 'slut' INTO OUTFILE 'test.txt' FIELDS TERMINATED BY '\t';

它可以完美地在 lib 文件夹中生成 2.5 MB 大小的文本文件,但其他代码如下,用于另一个查询,我必须从另一个表中添加一个列以及一些带有某些标题的空列,似乎可以工作,但产生的文件大小约为11GB,

sELECT t1.sku, t.name, ' ' as Size, format, ' ' as subcategory, CAST(t1.price AS UNSIGNED INTEGER) as price, ' ' as dummy, CONCAT('http://example.com/',t1.url_key) as url, CONCAT('Från ',t1.level_id_value) as shipping_fee, t2.is_in_stock as stock, ' ' as text1, ' ' as Description from t2,t1 where t1.delivery <> 'slut' INTO OUTFILE 'test.txt' FIELDS TERMINATED BY '\t';

调试此问题的任何帮助将是可观的,对mysql了解不多,因此非常欢迎详细解释。有没有一种方法可以使代码类似于两个表中的第一个工作代码?我只需要 t2 表中的一列,即 is_in_stock。

4

1 回答 1

1

所以第一个是带有 WHERE 子句的简单查询 SELECT INTO,这将返回少量结果。

第二个是显示一个隐式内部连接,但是您不是基于唯一行返回值,您只需从两个表中提取所有内容,无论它们在两个表中是否匹配

SELECT t1.sku, 
t.name, 
' ' as Size, 
format,
' ' as subcategory, CAST(t1.price AS UNSIGNED INTEGER) as price, 
' ' as dummy, CONCAT('http://example.com/',t1.url_key) as url, 
CONCAT('Från ',t1.level_id_value) as shipping_fee, 
t2.is_in_stock as stock, 
' ' as text1,
' ' as Description **`from t2,t1`** 
where t1.delivery <> 'slut' INTO OUTFILE 'test.txt' FIELDS TERMINATED BY '\t';

这就是我的做法,但您仍然需要隔离每条记录,所以是什么让每一行独一无二,并在 t1 和 t2 之间的那些字段上进行内部连接,如内部连接 ​​t2 ON t1.name = t2.name 和t1.id = t2.id 但它应该看起来像这样

SELECT t1.sku, 
t1.name, 
t1.format, 
CAST(t1.price AS UNSIGNED INTEGER) as price, 
CONCAT('http://example.com/',t1.url_key) as url,
CONCAT('Från ',t1.level_id_value) as shipping_fee, 
'' as dummy,
'' as size, 
'' as subcat,
'' as text1,
'' as Description,
t2.is_in_stock as stock,
from t1
INNER JOIN t2 WHERE t1.delivery <> 'slut'
INTO OUTFILE 'test.txt' FIELDS TERMINATED BY 't\';

希望能帮助到你!

于 2012-06-18T14:14:06.063 回答