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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题