Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php和mysql如何连接表_Php_Mysql - Fatal编程技术网

php和mysql如何连接表

php和mysql如何连接表,php,mysql,Php,Mysql,PHP&MySQL如何从一个表中检索多条记录以及从另一个表中检索相应的记录并一起显示 我有一个代码,它将我表中所有公司的所有购买价值相加(交易),并根据公司代码(购买公司代码)显示价值。我有另一个表(客户公司),它保存了我所有的公司和他们的代码。公司名称字段称为公司名称,而公司代码字段称为公司代码,这是交易表的购买公司代码字段的主要字段。我想从客户公司表中检索公司名称,并根据我从交易表中检索到的相应购买公司代码显示它们。如何编写PHP代码的第二部分。提前谢谢 <?php //code t

PHP&MySQL如何从一个表中检索多条记录以及从另一个表中检索相应的记录并一起显示

我有一个代码,它将我表中所有公司的所有购买价值相加(交易),并根据公司代码(购买公司代码)显示价值。我有另一个表(客户公司),它保存了我所有的公司和他们的代码。公司名称字段称为公司名称,而公司代码字段称为公司代码,这是交易表的购买公司代码字段的主要字段。我想从客户公司表中检索公司名称,并根据我从交易表中检索到的相应购买公司代码显示它们。如何编写PHP代码的第二部分。提前谢谢

<?php

//code that sums all the purchase value of all firms in my table (trades) and displays the value against the firm code(buy_firm_code)

$con=mysqli_connect("localhost","user","password","database");

// Check connection

if (mysqli_connect_errno())

{

echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$buy_firm_code="";         
$buy_value="";

$sql="SELECT buy_firm_code, SUM(trade_value) value_bought FROM trades GROUP BY buy_firm_code ORDER BY value_bought DESC";

if ($result=mysqli_query($con,$sql))
{

// Fetch one and one row

  while($row=mysqli_fetch_array($result)){

  $buy_firm_code=$row['buy_firm_code']; 
  $buy_value = $row['value_bought'];

  echo $buy_firm_code.'<-->'.$buy_value .'<br>';

}

// Free result set
 mysqli_free_result($result);
}
mysqli_close($con);

?>

将您的
sql
查询更改为此

$sql="SELECT t.buy_firm_code, f.firm_name, SUM(t.trade_value) value_bought 
    FROM trades t,client_firm f
    WHERE f.firm_code = t.buy_firm_code
    GROUP BY buy_firm_code ORDER BY value_bought DESC";
while

while($row=mysqli_fetch_array($result)) {
    $buy_firm_code=$row['buy_firm_code']; 
    $buy_value = $row['value_bought'];
    $firm_name = $row['firm_name'];
    echo $buy_firm_code.'<-->'.$firm_name.'<-->'.$buy_value .'<br>';
}
while($row=mysqli\u fetch\u数组($result)){
$buy_firm_code=$row['buy_firm_code'];
$buy_value=$row['value_buy'];
$firm_name=$row['firm_name'];
echo$buy_firm_code.'.$firm_name.'.$buy_value.'
'; }

如果它显示了您需要的内容,请告诉我

为什么不想将结果加入SQL查询?非常感谢。今天晚些时候,我将尝试“WHERE”和“JOIN”选项,并让你们知道进展如何。再次感谢各位。“Where”和“JOIN”都非常有效。我正面临着一个新的挑战,我希望在尽了最大努力后发布。不客气,如果答案对你有效,请将其标记为已接受