MySQL操作程序八

返回

搜索數(shù)據(jù)庫(search1.pl)

use strict; use DBI; my(%t,$n,@fld,@rec,$pref,$aref,%seen); # 連接數(shù)據(jù)庫 $$pref{dsn} = "DBI:mysql:host=localhost;database=cookbook"; $$pref{dbh} = DBI->connect($$pref{dsn}, "cbuser", "cbpass") or die "Cannot connect to server\n"; $$pref{dbh}->do("SET NAMES utf8"); if(!$$pref{dbh}){ print "SQL read ERROR!\n"; exit; } # 輸入數(shù)據(jù) print "Please input the search word="; chop($t{word1}=<STDIN>); # 從enq1中 search ourref and ENQ的編號 $aref = $$pref{dbh}->selectcol_arrayref("SELECT id FROM enq1 WHERE ourref LIKE \'\%$t{word1}\%\' OR ownerno LIKE \'\%$t{word1}\%\' ORDER BY id DESC"); @{ $t{list1} } = @$aref; # 從enq1.hullnoid=>hull_no.name search 船名 $aref = $$pref{dbh}->selectcol_arrayref("SELECT enq1.id FROM enq1,hull_no WHERE hull_no.name LIKE \'\%$t{word1}\%\' AND hull_no.id = enq1.hullnoid ORDER BY enq1.id DESC"); @{ $t{list2} } = @$aref; # 兩個搜索結(jié)果合并(可能有相同id的情況) @{ $t{list3} } = (@{ $t{list1} },@{ $t{list2} }); @{ $t{list4} } = grep { ! $seen{$_} ++ } @{ $t{list3} }; # 排序 @{ $t{list} } = reverse sort {$a<=>$b} @{ $t{list4} }; if ( $t{list}[0] ) { print "SEARCH OK\n"; print "@{ $t{list} }\n"; } else { print "SEARCH NG\n"; } # 關(guān)閉數(shù)據(jù)庫 $$pref{dbh}->disconnect;

舊搜索程序保存

$t{enq1_word} = ''; # main_type1のnameとDWGのsearch $t{sth} = $self->dbh->prepare("SELECT id, name, DWG FROM main_type1"); $t{sth}->execute; while ( @rec = $t{sth}->fetchrow_array ) { @{ $t{DWGs} } = split(/=/,$rec[2]); $t{DWG} = join(' ',@{ $t{DWGs} }); $t{name1} = $rec[1] . ' ' . $t{DWG}; $t{name1} = lc($t{name1}); if ( $t{name1} =~ /$t{word1}/ ) { $t{main_type1list}{$rec[0]}++; $t{SEARCH_OK} = 1; } } $t{sth}->finish; # enq1 id の獲得 if ( $t{SEARCH_OK} == 1 ) { @{ $t{main_type1s} } = keys %{ $t{main_type1list} }; for $n ( 0 .. $#{ $t{main_type1s} } ) { $t{main_type1id} = $t{main_type1s}[$n]; $t{sth} = $self->dbh->prepare("SELECT id,type1id FROM enq1"); $t{sth}->execute; while ( @rec = $t{sth}->fetchrow_array ) { @{ $t{name1s} } = split(/==/,$rec[1]); for $n1 ( 0 .. $#{ $t{name1s} } ) { if ( $t{main_type1id} == $t{name1s}[$n1] ) { $t{enq1list1}{$rec[0]}++; $t{SEARCH_OK} = 2; } } } $t{sth}->finish; } } if ( $t{SEARCH_OK} == 2 ) { $t{enq1_word} = 'TYPE_DWG_'; } # 各partsテーブル search $t{type1_leng} = $self->dbh->selectrow_array("SELECT count(*) FROM e1"); for $n ( 1 .. $t{type1_leng} ) { $t{ptable1} = sprintf("%06d",$n); $t{ptable1} = 'a' . $t{ptable1}; $t{count1} = $self->dbh->selectrow_array("SELECT count(*) FROM e1} WHERE code LIKE \'\%$t{word1}\%\'"); if ( $t{count1} > 0 ) { $t{main_type1list2}{$n}++; $t{SEARCH_OK} = 3; } } # enq1 id の獲得 if ( $t{SEARCH_OK} == 3 ) { @{ $t{main_type1s} } = keys %{ $t{main_type1list2} }; for $n ( 0 .. $#{ $t{main_type1s} } ) { $t{main_type1id} = $t{main_type1s}[$n]; $t{sth} = $self->dbh->prepare("SELECT id,type1id FROM enq1"); $t{sth}->execute; while ( @rec = $t{sth}->fetchrow_array ) { @{ $t{name1s} } = split(/==/,$rec[1]); for $n1 ( 0 .. $#{ $t{name1s} } ) { if ( $t{main_type1id} == $t{name1s}[$n1] ) { $t{enq1list2}{$rec[0]}++; $t{SEARCH_OK} = 4; } } } $t{sth}->finish; } } if ( $t{SEARCH_OK} == 4 ) { $t{enq1_word} .= '_PARTS_'; } # enq1のshipnameなどのサーチとenq1 idの獲得 $t{sth} = $self->dbh->prepare("SELECT * FROM enq1 ORDER BY id DESC"); $t{sth}->execute; while ( @rec = $t{sth}->fetchrow_array ) { $t{shipname} = $self->dbh->selectrow_array("SELECT name FROM WHERE id = $rec[5]"); $t{name1} = ' ' . $t{shipname}; $t{orderno} = $self->dbh->selectrow_array("SELECT orderno FROM HERE id = $rec[0] and orderno is not NULL"); $t{name1} .= ' ' . $rec[0] . ' ' . $rec[2] . ' ' . $rec[4]; if ( $t{orderno} ) { $t{name1} .= ' ' . $t{orderno}; } $t{name1} = lc($t{name1}); if ( $t{name1} =~ /$t{word1}/ ) { $t{enq1list3}{$rec[0]}++; $t{SEARCH_OK} = 5; } } $t{sth}->finish; if ( $t{SEARCH_OK} == 5 ) { $t{enq1_word} .= '_OURREF_'; } # enq1 のまとめと出力 if ( $t{SEARCH_OK} == 2 or $t{SEARCH_OK} == 4 or $t{SEARCH_OK} == 5 ) { @{ $t{enq1list1keys} } = keys %{ $t{enq1list1} }; @{ $t{enq1list2keys} } = keys %{ $t{enq1list2} }; @{ $t{enq1list3keys} } = keys %{ $t{enq1list3} }; @{ $t{enq1list} } = (@{ $t{enq1list1keys} },@{ $t{enq1list2keys} enq1list3keys} }); # 合并重復(fù)號 %seen = (); @{ $t{enq1lists} } = (); foreach $item (@{ $t{enq1list} }) { unless ( $seen{$item} ) { $seen{$item} = 1; push(@{ $t{enq1lists} },$item); } } @{ $t{enq1s} } = reverse sort {$a<=>$b} @{ $t{enq1lists} };
返回
观塘区| 扎囊县| 天祝| 内黄县| 镇远县| 定西市| 广西| 鹰潭市| 霸州市| 沾益县| 绍兴市| 汽车| 新疆| 深州市| 上饶市| 谢通门县| 新兴县| 常山县| 旬阳县| 芦溪县| 千阳县| 大丰市| 张家界市| 电白县| 如东县| 两当县| 大洼县| 应城市| 贺兰县| 宁河县| 响水县| 芮城县| 宁波市| 新安县| 合阳县| 鹰潭市| 开原市| 娱乐| 拉萨市| 神木县| 民乐县|