请求cakephp上的sql

时间:2017-07-26 02:57:48

标签: sql cakephp

我试图在cakephp上执行一个非常长且复杂的sql请求。从文档中我找到了2个解决方案。第一个是调用连接管理器并将我的请求作为字符串传递,第二个是通过php执行它。我的问题是请求非常复杂,我无法在php上找到正确的方法。

这是我的要求:

select * from flight_schedules as fs left join weekly_schedules as ws on fs.code = ws.code left join structures as s on fs.structure_ref = s.id
where fs.c_code = "xxx" 
and DATE_FORMAT(fs.flight_start, '%Y-%m-%d') <= DATE_FORMAT(NOW(), '%Y-%m-%d')
and DATE_FORMAT(NOW(), '%H:%i:%s') >= DATE_FORMAT(DATE_SUB(fs.flight_start,INTERVAL 1 HOUR) , '%H:%i:%s')
and DATE_FORMAT(NOW(), '%H:%i:%s') <= DATE_FORMAT(fs.flight_end, '%H:%i:%s')
and ( DATE_FORMAT(NOW(), '%Y-%m-%d') BETWEEN DATE_FORMAT(fs.flight_start, '%Y-%m-%d') and DATE_FORMAT(fs.flight_end, '%Y-%m-%d'))
or ( fs.flight_end IS NULL
and (DAYOFWEEK(NOW()) + ws.monday = 3
or DAYOFWEEK(NOW()) + ws.tuesday = 4
or DAYOFWEEK(NOW()) + ws.wednesday = 5
or DAYOFWEEK(NOW()) + ws.thursday = 6
or DAYOFWEEK(NOW()) + ws.friday = 7
or DAYOFWEEK(NOW()) + ws.saturday = 8
or DAYOFWEEK(NOW()) + ws.sunday = 2 ))
order by DATE_FORMAT(fs.flight_start, '%H:%i:%s') LIMIT 1

1 个答案:

答案 0 :(得分:0)

尝试这样的事情;

 "conditions" => [
    "fs.c_code" => "xxx",
    "DATE_FORMAT(fs.flight_start, '%Y-%m-%d') <= " =>  "DATE_FORMAT(NOW(), '%Y-%m-%d')",
    "DATE_FORMAT(NOW(), '%H:%i:%s') >= " =>  "DATE_FORMAT(DATE_SUB(fs.flight_start,INTERVAL 1 HOUR) , '%H:%i:%s')",
    "DATE_FORMAT(NOW(), '%H:%i:%s')" => "DATE_FORMAT(fs.flight_end, '%H:%i:%s')",
    "DATE_FORMAT(fs.flight_start, '%Y-%m-%d')  <= " => "(DATE_FORMAT(NOW(), '%Y-%m-%d')",
    "DATE_FORMAT(fs.flight_end, '%Y-%m-%d')  >= " => "(DATE_FORMAT(NOW(), '%Y-%m-%d')",
    "OR" => [
        "fs.flight_end" => null,
        [
            "DAYOFWEEK(NOW()) + ws.monday" => 3,
            "OR"=>[
                "DAYOFWEEK(NOW()) + ws.tuesday" => 4,
                "DAYOFWEEK(NOW()) + ws.wednesday" => 5,
                "DAYOFWEEK(NOW()) + ws.thursday" => 6,
                "DAYOFWEEK(NOW()) + ws.friday" => 7,
                "DAYOFWEEK(NOW()) + ws.saturday" => 8,
                "DAYOFWEEK(NOW()) + ws.sunday" => 2
            ]
        ],

    ]
],
//DO ORDER AND LIMIT HERE

抱歉,我忘了包装它。请检查更新一个。