SQL,在计算两次之间的差时插入表中

问题描述 投票:-2回答:2

我目前正在使用全日历制作一个简单的日历。我的数据库有三个日期时间:

start 
end
total

我想要的是该程序,因为它插入了日期,以计算开始和结束之间的差并将其加到总计中。这是我要插入的SQL代码:

我正在MySQL中使用InnoDB。

session_start();

include_once './conexao.php';

$dados = filter_input_array(INPUT_POST, FILTER_DEFAULT);

//Converter a data e hora do formato brasileiro para o formato do Banco de Dados
$data_start = str_replace('/', '-', $dados['start']);
$data_start_conv = date("Y-m-d H:i:s", strtotime($data_start));

$data_end = str_replace('/', '-', $dados['end']);
$data_end_conv = date("Y-m-d H:i:s", strtotime($data_end));

$query_event = "INSERT INTO events (title, name, color, start, end, total) "
        . "VALUES (:title, :name, :color, :start, :end) "
        . "SELECT DATEDIFF(':start', ':end') AS total";

$insert_event = $conn->prepare($query_event);
$insert_event->bindParam(':title', $dados['title']);
$insert_event->bindParam(':name', $dados['name']);
$insert_event->bindParam(':color', $dados['color']);
$insert_event->bindParam(':start', $data_start_conv);
$insert_event->bindParam(':end', $data_end_conv);

if ($insert_event->execute()) {
    $retorna = ['sit' => true, 'msg' => '<div class="alert alert-success" role="alert">Evento inserido com sucesso!</div>'];
    $_SESSION['msg'] = '<div class="alert alert-success" role="alert">Evento inserido com sucesso!</div>';
} else {
    $retorna = ['sit' => false, 'msg' => '<div class="alert alert-danger" role="alert">Erro: Evento não foi inserido com sucesso!</div>'];
}


header('Content-Type: application/json');
echo json_encode($retorna);```


php mysql sql pdo
2个回答
0
投票

只是:

INSERT INTO events (title, name, color, start, end, total) 
VALUES (:title, :name, :color, :start, :end, DATEDIFF(:start, :end) ) 

应该这样做。


0
投票

您忘了问一个问题或确切解释您在运行代码时遇到的问题!

但是幸运的是,仅通过查看您一定遇到了SQL语法错误-您就不能在同一查询中使用INSERT...VALUESINSERT...SELECT语法-您必须使用其中一种。但是您在这里不需要SELECT,您只需将DATEDIFF函数作为值之一即可。您也不需要在传递给DATEDIFF的参数周围加上引号。

您也不能在PDO中两次使用相同的参数名称。

我认为这样做应该更好(尽管显然我无法测试):

$query_event = "INSERT INTO events (title, name, color, start, end, total) "
        . "VALUES (:title, :name, :color, :start, :end, DATEDIFF(:start2, :end2))";

$insert_event = $conn->prepare($query_event);
$insert_event->bindParam(':title', $dados['title']);
$insert_event->bindParam(':name', $dados['name']);
$insert_event->bindParam(':color', $dados['color']);
$insert_event->bindParam(':start', $data_start_conv);
$insert_event->bindParam(':end', $data_end_conv);
$insert_event->bindParam(':start2', $data_start_conv);
$insert_event->bindParam(':end2', $data_end_conv);

P.S。如果您在运行代码时还有其他问题,则需要专门对其进行解释,因为一开始没有提到它们。


0
投票

不要在一个插入语句中混合Values子句和Select子句。您有两种选择:

$query_event = "INSERT INTO events (title, name, color, start, end, total) "
        . "VALUES (:title, :name, :color, :start, :end, DATEDIFF(':start', ':end')) "
$query_event = "INSERT INTO events (title, name, color, start, end, total) "
        . "SELECT :title, :name, :color, :start, :end DATEDIFF(':start', ':end') AS total from dual";
© www.soinside.com 2019 - 2024. All rights reserved.