dp815292 2014-03-07 18:56
浏览 66

如何在两个日期之间选择一条记录

I've a table that stores the reservations of rooms and for each room I want select the reservations, by a PHP date. Here is it the query:

SELECT id_prenotazione, id_alloggio, FROM_UNIXTIME(data_checkin,'%d/%m/%Y') AS checkin,
  FROM_UNIXTIME(data_checkout,'%d/%m/%Y') AS checkout, pagata, esclusiva
FROM prenotazioni 
WHERE id_alloggio = $id_alloggio
  AND FROM_UNIXTIME(data_checkin,'%d/%m/%Y') >= ".$data."
  OR FROM_UNIXTIME(data_checkout,'%d/%m/%Y') <= " .$data.") 
ORDER BY checkin ASC
LIMIT 1;

but if there are two or more reservations on the same room, the query always returns the first reservation, while if I change "ASC" with "DESC", returns only the last reservation. I want the single reservation of a specific room ($id_allogio) in a specific date($data). What am I doing wrong? Thanks in advance for the answer.

  • 写回答

3条回答 默认 最新

  • doulu2011 2014-03-07 19:04
    关注

    Why not using the between statement :

    SELECT col1,col2,col3,col4,blabla 
    FROM prenotazioni 
    WHERE 
    id_alloggio = $id_alloggio 
    and FROM_UNIXTIME(data_checkin,'%d/%m/%Y') between date_1 AND date_2
    ORDER BY checkin ASC
    LIMIT 1;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器