2011-07-24 27 views
1

我的MySQL數據庫如何總結2行PHP的MySQL

ID platos mhkos 
1 22 33 
2 15 12 

通過ID和結果=柏拉圖+ 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); 
?> 
+0

你的問題似乎還不清楚。你能舉一個你想要實現的例子嗎? –

+0

你的意思是如何得到兩列的總和?如果是,請編輯問題的標題。 – Sukumar

回答

1

它好像在OP的意思是兩列的總和一行。

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

如果OP希望所有的行和列的總和將是

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

的問題似乎相當混亂,雖然。因此,我傾向於最初的兩列和一列。

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

這隻會加一行,而不是2行。 CMIIW。 –

+0

請編輯你的問題,然後讓它更清晰你想要總結的東西;並根據你提供的數據,給出這個數字的答案。 – Mike