2011-07-24 7 views
1

私のMySQL DB方法を合計する2行のPHPのmysqlの

ID platos mhkos 
1 22 33 
2 15 12 

検索どのように?

私のコードは、どのようなOPが意味することは一列に2列の和であるかのように思え

<?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

2つの列の合計を取得する方法を意味しましたか?はいの場合は、質問のタイトルを編集してください。 – Sukumar

答えて

1

です。

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

OPは、すべての列と行の合計を望んでいるなら、それは問題はしかし、かなりの混乱に見えるん

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

だろう。したがって私は最初の2列と1列に向かって傾いています。

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

これは2行ではなく、1行だけを合計します。 CMIIW。 –

+0

あなたの質問を編集し、合計しようとしていることを明確にしてください。あなたが提供するデータがあれば、その合計に対する答えも与えます。 – Mike

関連する問題