doufu1939 2015-03-02 14:51
浏览 12
已采纳

什么是正确的查询?

Im trying to figure out how my query should look like.

With this query i select * from usr_time_reg
WHERE usr_time_nr = '$usr_time_nr' AND usr_stamp_in LIKE '$y_m'

So far, so good!
Now i'd like to also select usr_stamp_in WHERE usr_stamp_in = '0000-00-00 00:00:00' AND usr_stamp_out = '$y_m

How should that query look like?
Query downunder is selecting ALL usr_stamp_in that contains '0000-00...'
I still need the filtering with "time_nr & y_m"!

//Make the Select in mysql
$query = "SELECT * FROM usr_time_reg
                   WHERE usr_time_nr = '$usr_time_nr' AND usr_stamp_in LIKE '$y_m'
                   OR usr_stamp_in = '0000-00-00 00:00:00' AND usr_stamp_out LIKE '$y_m'
                   ORDER BY usr_stamp_in DESC";

Variables

$y_m = "2015-03%";
$time_nr = "1";

db

ID  time_nr        stamp_in                stamp_out
1    1        2015-03-03 06:00:00     2015-03-03 15:30:00
2    1        0000-00-00 00:00:00     2015-03-04 15:30:00
3    1        2015-03-03 06:00:00     0000-00-00 00:00:00
  • 写回答

1条回答 默认 最新

  • dptx8888 2015-03-05 14:50
    关注

    I think everything you are wanting to do can be done down in the WHERE statement.

    My best guess is:

    SELECT *
    FROM usr_time_reg
    WHERE 
        usr_time_nr = '$usr_time_nr'
        AND
        (
            usr_stamp_in LIKE '$y_m'    
            OR
            (
                usr_stamp_in = '0000-00-00 00:00:00' 
                AND 
                usr_stamp_out = '$y_m'
            )
        );
    

    Here usr_time_nr will always have to be '$user_time_nr' and then either one of the conditions seperated by OR will need to be true.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)