减去数组值

时间:2016-04-04 12:27:07

标签: php mysql arrays

我使用数组(通过PHP)从MySQL数据库获取数据。

我在想,是否可以减去数组值,就像这样(第7行):

$num = 30;

$result1 = mysql_query('SELECT * FROM table1');
$result2 = mysql_query('SELECT * FROM table2');

while($row1 = mysql_fetch_array($result1) && $row2 = mysql_fetch_array($result2)) {
    $sub = $row1['number'] - $row2['number'];
    if($sub<=$num) {
        echo $row1['person'];
}

我实际上没有得到任何结果(只是空白)。所以我想知道该行或代码的任何部分在逻辑上是否正确?

1 个答案:

答案 0 :(得分:1)

试试这个

$num = 30;

$result = mysql_query('SELECT number.table1 as n1, number.table2 as n2, colName.tableName FROM table1 JOIN table2 ON id.table1 = table1_id.table2');

while($row = mysql_fetch_array($result)) {
  $sub = $row['n1'] - $row['n2'];
  if($sub<=$num) {
    echo $row['person'];
  }
}