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条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试