Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.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
Mysql 如何合并/简化两个相同的查询_Mysql_Sql_Loops_Foreach - Fatal编程技术网

Mysql 如何合并/简化两个相同的查询

Mysql 如何合并/简化两个相同的查询,mysql,sql,loops,foreach,Mysql,Sql,Loops,Foreach,我有两个相同的问题 问题1: $request1 = $bdd->prepare('SELECT * from table1, table2 where table1.FK_license = table2.license and date = (select max(date) from table1 where user = :user)'); $request1-> execute(array( ':user' => $id )); 问题2: $request2

我有两个相同的问题

问题1:

$request1 = $bdd->prepare('SELECT * from table1, table2 where table1.FK_license = table2.license and date = (select max(date) from table1 where user = :user)');
$request1-> execute(array(
    ':user' => $id
));
问题2:

$request2 = $bdd->prepare('SELECT * from table1, table2 where table1.FK_license = table2.license and date = (select max(date) from table1 where user = :user)');
$request2-> execute(array(
    ':user' => $id
));

$test = $request2->fetch();
但我需要第一个查询来生成如下循环:

    <?php foreach ($request1 as $value) { ?>
        <tr>
            <td><?php echo $value['li']; ?></td>
            <td><?php echo $value['cu']; ?></td>
            <td><?php echo $value['qt']; ?></td>
            <td><?php echo $value['pk']; ?></td>
        </tr>
    <?php } ?>
我如何才能只使用一个查询来执行相同的操作?因为当我收到第一个请求后,我就不能再做foreach了

多谢各位

编辑:最终的代码看起来像


您只需使用mysql join连接两个表,我不需要连接,这是相同的,但如果我获取第一个表,我无法执行foreach,您必须使用以下两个表之间的foreach循环:$request2=$bdd->prepare'SELECT*from table1,table2其中table1.FK_license=table2.license and date=从table1中选择maxdate,其中user=:user'$request2->executearray':用户'=>$id;==foreach循环=====$test=$request2->fetch;::因此,您可以在获取每个循环之前获取使用的值。谢谢,但我认为我没有提供足够的详细信息,我编辑了post。这两个查询完全相同,我无法理解您所说的仅检索某一行的公共数据是什么意思。请提供样品数据和样品数据的预期输出,以便我们了解您的需求。
$test['data_for_group_by']
//This request allows me to create the loop to list all my articles
$request1 = $bdd->prepare('SELECT * from table1, table2 where table1.FK_license = table2.license and date = (select max(date) from table1 where user = :user)');
$request1-> execute(array(
    ':user' => $id
));


//With this query I retrieve a serial number (common to all articles) => $test['data_for_group_by']
$request2 = $bdd->prepare('SELECT * from table1, table2 where table1.FK_license = table2.license and date = (select max(date) from table1 where user = :user)');
$request2-> execute(array(
    ':user' => $id
));
$test = $request2->fetch();


//Here I count the number of article that I have
$license = $bdd->prepare('SELECT count(id) from table1 where number = :number');
$license ->execute(array(
    ":number" => $test['data_for_group_by']
    ));
$sum1 = $license->fetchColumn();


//And I recover the total weight of the articles (knowing that this details of weight this finds on another table)
$sum = $bdd->prepare('SELECT ROUND(SUM(pk), 2) from table1, table2 where table1.FK_license = table2.license and number = :number');
$sum ->execute(array(
    ":number" => $test['data_for_group_by']
    ));
$sum2 = $sum->fetchColumn();


?>

<p class="text-center">Quantity : <?php echo $sum1;?> - Weight : <?php echo $sum2;?> kg</p>

=== Foreach Loop ===