 |
ขอถามเกี่ยวการ insert ข้อมูลที่ไม่ซ้ำหน่อยครับ คือว่า... |
|
 |
|
|
 |
 |
|
ต้องกำหนดที่ Type ของชนิดข้อมูลค่ะ ให้เป็น PK อ่ะค่ะ
แต่ไม่รุว่าทำแบบอื่นได้หรือป่าวอ่ะคะ่
|
 |
 |
 |
 |
Date :
2012-04-11 11:57:44 |
By :
penpimonmink |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$Name=$_POST['Name'];
$Result=mysql_query("SELECT * FROM tb_member WHERE Name='".$Name."'") or die (mysql_error());
$Num_Rows=mysql_num_rows($Result);
if($Num_Rows==0){
echo "ท่านสามารถเพิ่มได้";
}else{
echo "มีรายละเอียดอยู่ในระบบแล้ว";
}
|
 |
 |
 |
 |
Date :
2012-04-11 12:43:00 |
By :
Ex-[S]i[L]e[N]t |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จาก คุณ Ex-[S]i[L]e[N]t No.2
ยังงัยก็ต้องเขียน Insert อีกทีอยู่ดี
|
 |
 |
 |
 |
Date :
2012-04-11 13:26:22 |
By :
aimoomoo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าแค่ มีข้อมูล แล้ว Insert ไม่ได้ หากไม่มี Inserได้
ก็ทำตาม No.2 ก็ได้แล้วนิครับ
ใช้ SQL ตัวเดียว
|
 |
 |
 |
 |
Date :
2012-04-11 13:41:26 |
By :
50121680 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือว่า..... ตอนนี้ขอมูล อยู่ประมาณ แสนกว่าเรคอด
เขียน select เพื่อหาข้อมูลซ้ำว่ามีไหม ถ้าไม่มี ก็มาเขียนคำสั่ง Insert อีกที
ที่ผมกลัวคือ กลัวมันช้าครับ ไม่มีจุดประสงอื่น เลยลองตั้ง กระทู้ถามดู เพื่อมีวิธี ดังที่กล่าวไว้ในหัวกระทู้
ปัญหาที่นี่ พอโปรแกรมอารายที่ทำงานช้าหน่อยก็เริ่มบ่น ผมเขีนโปรแกรมดเลยต้องหาวิธีที่ ซอฟ ที่สุด
|
ประวัติการแก้ไข 2012-04-11 13:47:49 2012-04-11 13:50:51
 |
 |
 |
 |
Date :
2012-04-11 13:47:06 |
By :
aimoomoo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตามความคิดเห็นของผมนะ ช้าหรือไม่ช้า มันเกี่ยวกับขนาดข้อมูลเยอะขนาดไหนมากกว่า
อย่างไรก็ตาม ถ้าจาทำการเช็คแต่ละครั้ง มันต้องวิ่งให้ครบทุกตัวอยู่แล้วหนิ
|
 |
 |
 |
 |
Date :
2012-04-11 15:50:43 |
By :
Ex-[S]i[L]e[N]t |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ delphi ado คงไม่ต่างกับ php มาหนักเรื่อง select
ประเด็นแปลกคือ 2 ฟังก์ชั่นนี้ต่างกันตรงใช้ WHERE 1 ตัว กับ 2 ตัว
ผมจะเพิ่มฐานข้อมูลหาก 2 เงื่อนไขไม่มีในฐาน
หากใช้ฟังก์ชั่น selete_ADO ต้องเรียกใช้ 2 ครั้ง
หากใช้ฟังก์ชั่น selete_ADO2 ต้องเรียกใช้ 1 ครั้งเท่านั้น
การเรียกใช้ selete_ADO 2ครั้ง ทำงานไวกว่า selete_ADO2
selete_ADO ใช้เวลา 2.12 นาที
selete_ADO2 ใช้เวลา 4.20 นาที
Code (PHP)
Function TForm3.selete_ADO(const ta,va:string):boolean;
var I:integer;
begin
with ADOQuery1 do
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select '+ ta);
ADOQuery1.SQL.Add('from songx');
ADOQuery1.SQL.Add('where ' + ta +' = ' + QuotedStr(va));
ADOQuery1.Open;
I:=ADOQuery1.RecordCount;
if (I = 0) then
begin
Result := False;
count5:=count5+1;//ไม่ซ้ำ
end;
if (I > 0) then
begin
Result := True;
Count6:=Count6+1; //ซ้ำ
end;
ADOQuery1.Close;
end;
end;
Function TForm3.selete_ADO2(const ta1,va1,ta2,va2:string):boolean;
var I:integer;
begin
with ADOQuery1 do
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('SELECT COUNT(*) as xxxx');
ADOQuery1.SQL.Add('from songx');
ADOQuery1.SQL.Add('where ' + ta1 +' = ' + QuotedStr(va1));
ADOQuery1.SQL.Add('AND ' + ta2 +' = ' + QuotedStr(va2));
ADOQuery1.Open;
I:=ADOQuery1.FieldByName('xxxx').AsInteger ;
if (I = 0) then
begin
Result := False;
count5:=count5+1;//ไม่ซ้ำ
end;
if (I > 0) then
begin
Result := True;
Count6:=Count6+1; //ซ้ำ
end;
ADOQuery1.Close;
end;
end;
|
ประวัติการแก้ไข 2012-10-12 03:06:50
 |
 |
 |
 |
Date :
2012-10-12 03:02:55 |
By :
bonparadorn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าเป็นการทำงานที่ไม่ได้ถูกสั่งหรือบังคับมาว่าต้องทำการ insert แค่ครั้งเดียว
ผมว่าให้มีคิวรี่ก่อนไม่เป็นไรหรอกครับมันไม่ช้ามากเท่าไรครับ ง่ายต่อการเขียนและใช้งานได้ง่าย ๆ ....
|
 |
 |
 |
 |
Date :
2012-10-12 06:41:21 |
By :
apisitp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่างของผมครับ ไม่รู้ว่าจะใช้กับ DB ของท่านได้หรือเปล่า
คำสั่ง Not IN ถ้ามีแล้วในตารางนั้นก็ไม่ต้อง Insert
insert into promotionreserve
(itemcode,itemname,proprice,unitcode,procode)
select itemcode,itemname,PriceperUnit,unitcode,'pro-085'
from BCPromotionSub where Code='pro-085' and ItemCode not in (select ItemCode from
promotionreserve)
order by itemcode
|
 |
 |
 |
 |
Date :
2017-03-20 13:03:02 |
By :
attadev.net |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|