【问题标题】:case statement in INSERT INTO statement [closed]INSERT INTO 语句中的 case 语句[关闭]
【发布时间】:2015-06-14 22:22:22
【问题描述】:

我有以下 register.php 代码

<?php
if( $_POST )
{
  include("php_connect/php_connect.php");

  $start_year  = $_POST['start_year'];
  $start_month = $_POST['start_month'];
  $start_day = $_POST['start_day'];
  $start_hour = $_POST['start_hour'];
  $start_minute = $_POST['start_minute'];
  $place = $_POST['place'];
  $salah = $_POST['salah'];
  $rakat = implode(",", $_POST['rakat']); 

  $query = "INSERT INTO performed_salah 
           (year, month, date, time, place, prayer, rakat, reg_date) VALUES 
           ('$start_year', '$start_month', '$start_day', ('$start_hour:$start_minute'), '$place', '$salah', '$rakat', NOW())";

  if ($conn->query($query) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $query . "<br>" . $conn->error;
}

$conn->close();
}
?>

在我的表中,我有一个列性能。 我正在尝试创建一个类似于

的 CASE STATEMENT
CASE WHEN prayer='A' and rakat=('Rakat-A,Rakat-B') then performance = 100%
     WHEN prayer='A' and rakat=('Rakat-A') or rakat=('Rakat-B') then performance = 50%
     WHEN prayer='B' and rakat=('Rakat-A,Rakat-B,Rakat-C,Rakat-D) then performance = 100%
WHEN prayer = 'B' and rakat=('Rakat-A,Rakat-B,Rakat-C') then performace = 75%
WHEN prayer = 'B' and rakat=('Rakat-A,Rakat-B') then performace = 50%
WHEN prayer = 'B' and rakat=('Rakat-A') then performace = 25%

我该怎么做呢?

我试过了,但出错了:

"INSERT INTO performed_salah('$start_year', '$start_month', '$start_day', ('$start_hour:$start_minute'), '$place', '$salah', '$rakat', 
(case when prayer = 'fajar' and (rakat='2Sunnat,2Faradh') then " . '100%' . "    
      when prayer = 'fajar' and (rakat='2Sunnat' or rakat='2Faradh') then " . ' '50%' . "end) '$performance', NOW())";

【问题讨论】:

  • “我试过了,但出错了:” - 存在?
  • 另外,我假设 '$start_year', '$start_month', '$start_day'... 是列。
  • year, month, date, time 为什么不使用一个完整的日期?

标签: php mysql insert case insert-into


【解决方案1】:

这是您的查询:

INSERT INTO performed_salah(year, month, date, time, place, prayer, rakat, reg_date)
     VALUES  ('$start_year', '$start_month', '$start_day', ('$start_hour:$start_minute'), '$place', '$salah', '$rakat', NOW());

这是有道理的。您的第二个查询根本没有意义。但是,一个基本问题是case 语句不能使用表中的列名。您可以在事后使用update 执行此操作,但在插入时,您需要用于插入的变量。像这样的:

INSERT INTO performed_salah(year, month, date, time, place, prayer,
            rakat, reg_date, performance)
     VALUES  ('$start_year', '$start_month', '$start_day', ('$start_hour:$start_minute'),
              '$place', '$salah', '$rakat',
               (case when '$prayer' = 'fajar' and '$rakat' = '2Sunnat,2Faradh'
                     then '100%'   
                     when '$prayer' = 'fajar' and '$rakat' in ('2Sunnat', '2Faradh')
                     then '50%' 
               end),
              NOW());

还有很多需要改进的地方。像performance 这样的字段看起来像一个数字,所以你应该这样存储它。如果使用参数而不是显式地将值放入字符串中,SQL 代码会更安全。但这可能会让您对如何进行有所了解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-04
    • 2021-01-24
    • 2015-02-21
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多