2017-05-01 29 views
0

我正在学习SQL,我处于必须将值插入数据库的情况下,只有当它不存在。只有插入时,如果唯一(检查2行)

我的tableview结构是这样的:

+----------+-----------+-----+-----+ 
| first_id | second_id | timestamp | 
+----------+-----------+-----------+ 

我想只有插入,如果没有相同first_idsecond_id例如,如果在桌子上有first_id 1second_id 2,我重新加入,我不想再添加它。所以如果first_id和second_id行已经有值1和2,那么不要添加,但如果first_id是3并且second_id是1,那么我会允许插入。

这是我的查询ATM:

INSERT INTO `testtable`.`ids` (`first_id`, `second_id`) VALUES (:first_id, :second_id) 

而且这样我试着用NOT EXISTS但它不工作:

NOT EXISTS (SELECT first_id, second_id FROM `testtable`.`ids` WHERE first_id = : first_id AND second_id = : second_id) INSERT INTO `testtable`.`ids` (`first_id `, `second_id `) VALUES (: first_id, : second_id) 

最后提到的查询给我的语法错误,但一旦我甚完整性违规,并告诉我检查文件。

我正在执行我的查询使用PHP ->query("");函数。

我试图像IF NOT EXISTSNOT EXISTS那样做,但那些都不起作用。我应该如何解决这个问题?

+0

如果'first_id'为1且'second_id'为1或3,您可以允许插入吗?它是否必须是2才不插入? – Sajal

+0

是的,如果这些值现在像'first_id'是2并且'second_id'是4那么我会允许的。 –

回答

4

这很简单。声明first_idsecond_idcomposite key。我不想在你的PHP代码中做任何修改,但是让你的数据库结构变得多功能,这样它就不会接受任何重复的值,只要你插入它。

CREATE TABLE `demo` (
    `first_id` smallint(6) DEFAULT NULL, 
    `second_id` smallint(6) DEFAULT NULL, 
    `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    UNIQUE KEY `first_id` (`first_id`,`second_id`) 
) 

现在first_idsecond_id绝不会接受重复的值。 enter image description here

^表包含值(1,2)。现在插入(1,3)。

enter image description here

^表接受(1,3)。现在再次插入(1,2)。

enter image description here

insert语句引发错误。现在,该表永远不会接受该键的重复值(first_id,second_id)。

如果表已经存在,你不是从头开始创建它,只需执行:

alter table `table_name` add unique key (first_id, second_id); 

这将防止重复值从此。

+0

这是最好的:)谢谢。但你能告诉我怎样才能抓住这个?如果发生这种情况,我不想得到内部消息。而不是500我想获得400或其他东西。 –

+0

我的荣幸:)高兴地帮助。 –

0

检查第一个和第二个ID是否存在,并另外为第一个和第二个ID声明2个SQL变量并根据需要设置它们。插入

DECLARE @Exists int; @first_id int; @second_id int; 
SET @first_id = 1; 
SET @second_id = 2; 
SELECT @Exists = COUNT(*) FROM [testtable] where [first_id] = @first_id and [second_id] = @second_id; 

条件,如果匹配的记录数为0:

IF(@Exists = 0) 
BEGIN 
    INSERT INTO [testtable](first_id, second_id) 
    VALUES(@first_id,@second_id) 
END 
+0

我现在听起来很愚蠢,但如果我使用PHP' - > query(“”)'函数来执行查询,那么我应该如何添加它? –

+0

我不知道如何做到这一点在PHP中,我是一个.net专业自己。抱歉。 – Sajal

1

如果你正在使用PHP和MySQL你可以试试这个:

<?php 

    //Added database connection code here 

    $first_id = $_POST['first_id ']; 
    $second_id = $_POST['second_id ']; 


    $sql = "select * from ids where first_id = ".$first_id ." and second_id ='".$second_id."'" ; 
    $result = $mysqli->query($sql); 
    $row = $result->fetch_row(); 

    if($row[0]) { 
     $mysqli->close(); 
    } else { 
     //preapare an insert statement 
     $stmt = $mysqli->prepare(" INSERT INTO `ids` (first_id, second_id) VALUES (?,?)"); 
     $stmt->bind_param("ii", $first_id, $second_id); 

      //execute the statement 
      if($stmt->execute()) { 
       unset($first_id); 
       unset($second_id); 
      } else { 
       echo $mysqli->error; 
      }  
     //close statement 
     $stmt->close();  
     $mysqli->close(); 
    } 
?> 
相关问题