doujiaochan7317 2013-08-28 20:40 采纳率: 0%
浏览 39
已采纳

根据另一个表的列值从表中选择记录

I know forming this query is trivial, but I am not able frame the exact query. Based on what I have read so far, the solution may include joining two tables. Here is the problem :

TABLE_ONE {S_NO, DK_NO, FRM , DATE}

TABLE_TWO {S_NO, DK_NO , TO}

I want to select a record from TABLE_ONE based on an input value of DK_NO and value checked for TO field of TABLE_TWO or FRM field of TABLE_ONE

unsuccessful trials so far:

1)

$sql = "select * from TABLE_ONE where DK_NO like '%".$dkno."%' and (FRM='".$department."' or  (TABLE_TWO.DK_NO like '%".$dakno."%' and TABLE_TWO.TO='".$department."'))";

2)

$sql = "select * from TABLE_ONE where DK_NO like '%".$dkno."%' and (FRM='".$department."' or   TABLE_TWO.TO='".$department."')";
  • 写回答

2条回答 默认 最新

  • doujiangao4229 2013-08-28 20:48
    关注

    Recommended reading: JOIN.

    Using inner join:

    "select t1.* 
    from table_one t1 inner join table_two t2 on t1.dk_no = t2.dk_no
    where (t1.frm = '". $department. "' or t2.to = '".$department. "') 
        and t1.dk_no like '%" . $dkno . "%'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置