简体   繁体   English

SQL,在计算两次差异时插入表

[英]SQL, insert into table while calculating difference between two times

I'm currently using fullcalendar to make a simple calendar.我目前正在使用 fullcalendar 制作一个简单的日历。 My database has got three date-times:我的数据库有三个日期时间:

start 
end
total

What I want is the program, as it inserts the date, to calculate the difference between start and end and add it to total.我想要的是程序,因为它插入日期,计算开始和结束之间的差异并将其添加到总数中。 This is my SQL code for insertion:这是我的插入 SQL 代码:

I'm using InnoDB in MySQL.我在 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);```


I can see just from looking that you must be experiencing a SQL syntax error - you can't use INSERT...VALUES and INSERT...SELECT syntax in the same query - you must use one or the other.我可以看出,您一定遇到了 SQL 语法错误 - 您不能在同一个查询中使用INSERT...VALUESINSERT...SELECT语法 - 您必须使用其中一个。 But you don't need the SELECT here, you can just put the DATEDIFF function as one of the values.但是这里不需要SELECT ,您可以将 DATEDIFF 函数作为值之一。 You also don't need quote marks round the parameters you pass to DATEDIFF.您也不需要在传递给 DATEDIFF 的参数周围加上引号。

You also can't use the same parameter name twice in PDO.您也不能在 PDO 中两次使用相同的参数名称。

This should work better I think (although I'm not able to test it, obviously):我认为这应该会更好(尽管我显然无法对其进行测试):

$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);

PS If there are still other issues when you run the code you will need to explain it specifically, since you didn't mention them at the start. PS 如果在运行代码时还有其他问题,您需要具体说明,因为您在开始时没有提及它们。

[EDIT] I think this could do it : [编辑] 我认为这可以做到:

INSERT INTO events (title, name, color, start, end, total) 
SELECT :title, :name, :color, t.start, t.end, DATEDIFF(t.start, t.end) )
FROM ( SELECT :start, :end) AS t

But you'll need to try to be sure.但你需要尝试确定。

Don't mix the Values clause and the Select clause in one insert statement.不要在一个插入语句中混合使用 Values 子句和 Select 子句。 You have two alternatives:您有两种选择:

$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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM