dpm91915 2014-11-30 00:00
浏览 8

选择具有多个更高和更低值的Mysql

i am in need of a search query based on 6 values from 3 columns day, month, year, right now i have got it some how working only for month and year but i can not get the day to perform correctly.

for example if some one wants to search from day, month year to day month year. my current query

    //From
    list($fy,$fm,$fd)   = explode ('-', 2013-2-20);
    //to
    list($ty,$tm,$td)   = explode ('-', 2014-9-1);

    $add = " AND 
            ( month >= '".$fm."'
            AND year >='".$fy."') AND ( 
            month <= '".$tm."' 
            AND year <=   '".$ty."'
            ) ";

as you can see the day is not included, your time is much appreciated.

  • 写回答

1条回答 默认 最新

  • dongzhaobai5982 2014-11-30 01:46
    关注

    Going on with what you have. You would just add day in the same way you added month and year:

    ...
    $add = " AND 
            ( month >= '".$fm."'
            AND year >='".$fy."'
            AND day >='".$fd."') AND ( 
            month <= '".$tm."' 
            AND year <=   '".$ty."'
            AND day <=   '".$td."'
            ) ";
    ...
    

    Since all the items are independent of each other, this works of the literal values are higher/lower in each item. Which is probably not what you want. You will need to convert the strings '2013-2-20' into DATE and then use those. This can be done with str_to_date(). So for example str_to_date('2013-2-20','%Y-%m-%d').

    Notice: I'm aware that you cannot change the database but for future references it's a good idea to have a single DATE datatype for your database. It's not good design to separate them into separates things such as day, month, and year. As with using the DATE datatype it can easily find lower and higher dates by simply comparing the dates (along with date functions using dates can be used):

    dateObj1 >= dateObj2 // dateObj1 is at a later date than dateObj2
    
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程