>  Q&A  >  본문

테이블이 없으면 테이블에 삽입

<p>사용자가 회로에 일련 번호(단위)를 추가할 수 있는 응용 프로그램이 있습니다. 셀 ID가 이미 존재하는지 확인하기 위해 두 테이블에 대한 삽입 쿼리를 수정하려고 합니다. 존재하지 않으면 삽입하고 싶지만 존재한다면 새 레코드를 삽입하고 싶지 않습니다. 나는 이와 관련하여 SO에서 찾은 답변을 검색하고 적용하려고 시도했지만 성공하지 못했습니다.</p> <p>这是나의 대표자,以 控器</p> <pre class="brush:php;toolbar:false;">공용 함수 AddNewCell() { if ($_SERVER['REQUEST_METHOD'] == 'POST') { $circuitId = $_POST["circuitId"]; $cellNum = filter_var($_POST["cellNum"], FILTER_SANITIZE_STRING); $toteId = $_POST["toteId"]; $posId = $_POST["posId"]; $stageCheckId = $this->GetStageIdByBatId($cellNum); if (비어 있음($stageCheckId)) { echo json_encode("0"); } 또 다른 { $cellId = $this->form->InsertNewCell($circuitId, $stageCheckId, $toteId, $posId); $this->wk->InsertCell($circuitId, $cellId, $cellNum, $toteId, $posId); echo json_encode($cellId); } } }</pre> <p>编队模型</p> <pre class="brush:php;toolbar:false;">공개 함수 InsertNewCell($circuitId, $stageCheckId, $toteId, $posId) { $this->db->query("INSERT INTO tbl_Cell_Tote_Track(Circuit_Id, Stage_Check_Id, Tote_Id, Position_Id) VALUES(:cid, :scid, :tid, :pid)"); $this->db->bind(":cid", $circuitId); $this->db->bind(":scid", $stageCheckId); $this->db->bind(":tid", $toteId); $this->db->bind(":pid", $posId); $this->db->execute(); $this->db->query("SELECT TOP(1) Cell_Id FROM tbl_Cell_Tote_Track ORDER BY Cell_Id DESC"); return $this->db->single()->Cell_Id; }</pre> <p>工作表模型</p> <pre class="brush:php;toolbar:false;">공용 함수 InsertCell($circuitId, $cellId, $cellNum, $toteId, $posId) { $this->db->query("SELECT Circuit_Num FROM tbl_Circuit_Track WHERE Circuit_Id = :cid"); $this->db->bind(":cid", $circuitId); $circuitNum = $this->db->single()->Circuit_Num; $position = $this->GetCellPos($toteId, $posId); $this->db->query("INSERT INTO tbl_OCV_Worksheet(Cell_Id, Circuit_Id, Circuit_Num, Position_Num, Serial_Num) VALUES (:clid, :cirid, :cn, :pn, :cnum)"); $this->db->bind(":clid", $cellId); $this->db->bind(":cirid", $circuitId); $this->db->bind(":cn", $circuitNum); $this->db->bind(":pn", $position); $this->db->bind(":cnum", $cellNum); $this->db->execute(); }</pre> <p>我尝试通过添加에서 Cell_Id 列上添加唯一约束 <code>$this->db->query("更改表 tbl_Cell_Tote_Track 添加唯一的 (Cell_Id);</code> 到模型函数,但当使用现有序列号输入单项。我也尝试过</p> <pre class="brush:php;toolbar:false;">공개 함수 InsertNewCell($circuitId, $stageCheckId, $toteId, $posId) { $this->db->query("tbl_Cell_Tote_Track에 삽입(Circuit_Id, Stage_Check_Id, Tote_Id, Position_Id) SELECT $circuitId, $stageCheckId, $toteId, $posId 존재하지 않는 곳(tbl_Cell_Tote_Track에서 Cell_Id 선택)'); $this->db->execute(); $this->db->query("SELECT TOP(1) Cell_Id FROM tbl_Cell_Tote_Track ORDER BY Cell_Id DESC"); return $this->db->single()->Cell_Id; }</pre> <p>这似乎可以防止该表出现复, 但一位高级同事告诉我这是不正确的. 굉장한 赞赏.如果需要包含更多代码,请告诉我。</p>
P粉546138344P粉546138344383일 전550

모든 응답(1)나는 대답할 것이다

  • P粉436688931

    P粉4366889312023-09-06 15:11:58

    select 语句上设置 where에 있는 경우

    와 같은 마지막 코드(선택 포함)를 선택할 수 있습니다. 으아악

    그리고 송신 유닛 ID의 기능 매개변수를 변경합니다.

    Cell_Id이 자동 증가인 경우 다른 열을 사용하여 제약 조건을 정의해야 합니다.

    회신하다
    0
  • 취소회신하다