doqs8936 2013-04-03 00:36
浏览 33
已采纳

mysql中不同查询的多个表

Originally my sql query works to check if there is pending timesheet/overtime ready for manager to review... now i added in on-call timesheets which is on a different table...

my previous sql query that worked but WITHOUT $ONCALL_TABLE...

list($qh, $num) = dbQuery("SELECT manager FROM $USER_TABLE WHERE username IN (SELECT DISTINCT uid FROM $TIMES_TABLE WHERE submitstatus=1 OR ot_status=1)");

now im trying to add the pending on-call timesheet to the same sql query but i keep getting errors... maybe someone can help me fix my syntax?:

list($qh, $num) = dbQuery("SELECT manager FROM $USER_TABLE WHERE username IN (SELECT DISTINCT $TIMES_TABLE.uid, $ONCALL_TABLE.uid FROM $TIMES_TABLE, $ONCALL_TABLE WHERE $TIMES_TABLE.submitstatus=1 OR $TIMES_TABLE.ot_status=1 OR $ONCALL_TABLE.submitstatus=1)");

ERROR I GET: Can't perform query: Operand should contain 1 column(s)

  • 写回答

1条回答 默认 最新

  • douying2243 2013-04-03 01:11
    关注

    The problem is in the subquery, but I would handle it using a union all:

    SELECT manager
    FROM $USER_TABLE
    WHERE username IN (SELECT uid
                       from (select $TIMES_TABLE.uid
                             from $TIMES_TABLE
                             where $TIMES_TABLE.submitstatus=1 OR $TIMES_TABLE.ot_status=1
                             union all
                             select $ONCALL_TABLE.uid
                             from $ONCALL_TABLE
                             where $ONCALL_TABLE.submitstatus=1
                            ) t
                       ) 
    

    To be honest, though, the union all may interfere with with the use of indexes. This might perform better:

    SELECT manager
    FROM $USER_TABLE
    WHERE username IN (select $TIMES_TABLE.uid
                       from $TIMES_TABLE
                       where $TIMES_TABLE.submitstatus=1 OR $TIMES_TABLE.ot_status=1
                      ) or
          username in (select $ONCALL_TABLE.uid
                       from $ONCALL_TABLE
                       where $ONCALL_TABLE.submitstatus=1
                      ) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用sql语句实现下面两个实验(需要代码和运行结果截图)
  • ¥20 用web解决,要给我一个完整的网页,符合上述的要求
  • ¥20 求个sql代码和结果的图 两道题
  • ¥15 银河麒麟操作系统无法使用U盘
  • ¥100 寻找:光电二极管电路设计服务
  • ¥15 YOLOv5改进后的结构图
  • ¥15 全志v3s怎么设置高速时钟,使用的荔枝派zero开发板,串口2需要921600的波特率
  • ¥15 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁
  • ¥15 bywave配置文件写入失败