doumianfeng6979 2014-05-30 21:59
浏览 376
已采纳

如果另一个表中不存在记录,则插入一个表

How can I club this two queries into one? Insert a record if a record does not exist in another table. I tried googling but I have only found solutions using joins, not in but here I guess there is no relation between this two tables. can I still insert into it with a single query

$res = mysqli_query($con,"SELECT 1 FROM bicds WHERE (bid = $pid] AND uid = $eid)
       OR (bid = $eid AND uid = $pid) LIMIT 1");

if(mysqli_num_rows($res) == 0){
    mysqli_query($con,"Insert into t1 (pid,ust) values ($pid,$ust)");
 }

Any help is greatly appreciated. Thanks

  • 写回答

2条回答 默认 最新

  • dongmen1860 2014-05-30 22:05
    关注

    You can do this using insert . . . select:

    Insert into t1(pid, ust) 
        select $pid, $ust
        from dual
        where not exists (select 1 from bicds where bid = $pid AND uid = $eid);
    

    This allows you to include a where clause in the insert.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?