簡體   English   中英

如何總結2行的PHP MySQL的

[英]how to sum 2 rows php mysql

我的MySQL數據庫

ID platos mhkos 
1    22    33
2    15   12

通過ID搜索結果= platos + mhkos如何?

我的代碼是

    <?php require_once('Connections/hlios.php'); ?>
<?php echo $row_test['platos']; ?>+ <?php echo $row_test['mhkos']; ?>

= <?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_test = "-1";
if (isset($_GET['client_id'])) {
  $colname_test = $_GET['client_id'];
}
mysql_select_db($database_hlios, $hlios);
$query_test = sprintf("SELECT * FROM carpets_1 WHERE id = %s", GetSQLValueString($colname_test, "int"));
$test = mysql_query($query_test, $hlios) or die(mysql_error());
$row_test = mysql_fetch_assoc($test);
$totalRows_test = mysql_num_rows($test);


mysql_free_result($test);
?>

好像OP的意思是在一個ROW中兩個COLUMNS的總和。

SELECT platos + mhkos AS totals FROM carpets_1 WHERE id = %s

如果OP想要所有列和行的總和,它將是

SELECT sum(platos) as totalPlatos, sum(mhkos) as totalMhkos FROM carpets_1

不過,這個問題似乎確實令人困惑。 因此,我傾向於最初的兩列和一行。

SELECT platos + mhkos AS totals FROM carpets_1 WHERE id = %s

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM