2015-07-10 69 views
0
SELECT * 
FROM `amc_info` 
WHERE department =' ( 
SELECT DISTINCT department) 
into outfile = 'Different department' 

我在amc_info中使用了大量數據,並使用不同的部門。一個部門可能有多個行,總部門超過30個,現在我想用部門方式的文件名導出這些數據。如何將DISTINCT數據導出到DISTINCT文件中

+0

您不能創建多個出文件從一個單一的查詢。考慮使用任何腳本來做到這一點。 – HashSu

+0

您是否擁有該類型的腳本,或者您可以幫助我導出該腳本。 – Pradeep

回答

0

我覺得這是你想達到什麼目的:

SELECT * INTO OUTFILE 'file.txt' 
FROM `amc_info` WHERE department IN (SELECT DISTINCT department FROM `amc_info`) 

退房的SELECT INTO語法太:https://dev.mysql.com/doc/refman/5.1/en/select-into.html

如果你只是試圖讓DISTINCT部門但是:

SELECT DISTINCT department INTO OUTFILE 'file_name.txt' 
FROM `amc_info` 
0

我米使用perl腳本 請確保您有Perl安裝DBI,DBD模塊安裝

use DBI; 
use strict; 
use warnings; 


my $dbh = DBI->connect("DBI:mysql:DBNAME;host=localhost", 'root', 'password'); 
my $sth = $dbh->prepare("select distinct(department) from amc_info"); 
$sth->execute(); 
while (my $depart= $sth->fetchrow_array()) { 

    my $sth1 = $dbh->prepare("select * into outfile '$depart\.txt' fields terminated by ',' lines terminated by '\n' from amc_info where department='$depart'"); 
    $sth1->execute(); 
    $sth1->finish(); 
} 

$sth->finish(); 

注意:更改此腳本並添加您的數據庫名稱,用戶名和密碼。如果使用遠程數據庫服務器提供遠程IP以代替localhost

第一選擇是讓你的部門 第二個選擇是爲每個部門建立檔案,並張貼他們各自部門的數據