在 PHP While 循环中使用 MySQL JOIN 而不是查询

Posted

技术标签:

【中文标题】在 PHP While 循环中使用 MySQL JOIN 而不是查询【英文标题】:Use MySQL JOIN instead of a query in PHP While loop 【发布时间】:2013-02-04 12:09:12 【问题描述】:

我正在尝试从 php while 循环中删除 mysql 查询并改用 JOIN。

我有一个问题

SELECT *, 
SUM(CASE WHEN posmonth like '2013%' THEN ext ELSE NULL END) AS year2013, 
SUM(CASE WHEN posmonth like '2012%' THEN ext ELSE NULL END) AS year2012, 
SUM(CASE WHEN posmonth like '2011%' THEN ext ELSE NULL END) AS year2011, 
SUM(CASE WHEN posmonth like '2010%' THEN ext ELSE NULL END) AS year2010 
FROM combined c 
LEFT JOIN customermaster cm ON c.alias=cm.customerkey 
WHERE cm.company LIKE 'A%' 
GROUP BY customerkey 
ORDER BY cm.company ASC 
LIMIT 0, 50

然后根据这些结果从另一个表“areamaster”中获取数据 使用:

    <?php 
$result = mysqli_query($link, $query);
   while ($r = mysqli_fetch_object($result)) 
    $alias = $r->alias;
    $company = $r->company;
    $a = mysqli_query($link, "SELECT area FROM areamaster WHERE areakey='$alias' LIMIT 1");
    while ($t = mysqli_fetch_object($a))  $aliasdisplay = $t->area; 
    //Instead of this ^^ USE JOIN IN ORIGINAL QUERY
    ?>
    <tr>
         <td><?php print (substr($company,0,20)); ?></td>
         <td><?php print (strtoupper($r->location)); ?></td>
         <td><?php print $aliasdisplay; ?></td>
         <td>$<?php print number_format($r->$year4V,2,".",","); ?></td>
         <td>$<?php print number_format($r->$year3V,2,".",","); ?></td>
         <td>$<?php print number_format($r->$year2V,2,".",","); ?></td>
         <td>$<?php print number_format($r->$year1V,2,".",","); ?></td>
   </tr>
   <?php
   
   mysqli_free_result($result);
    ?>

两个mysql表结构:

areamaster:
     areakey: Primary
     area: What I want displayed

combined:
     id:primary
     posmonth: for totaling ext by Year
     alias: compare to areamaster.areakey and get "areamaster.area"
     location: 
     ext: being totaled by SUM

如果有人可以帮助编写 JOIN,我将不胜感激。

另外,如果有任何进一步优化此查询的方法,请告诉我。

【问题讨论】:

尝试YEAR(posmonth) 而不是匹配您正在执行的字符串。执行字符串版本会强制 mysql 将其内部日期/时间格式重新格式化为字符串,而不是直接戳该字段的本机结构内的年份位。当然,这假设 posmonth 是本地日期/时间字段。 刚刚处理好了,谢谢 Marc 【参考方案1】:

这样试试

SELECT 
    *, 
    SUM(CASE WHEN posmonth like '2013%' THEN ext ELSE NULL END) AS year2013, 
    SUM(CASE WHEN posmonth like '2012%' THEN ext ELSE NULL END) AS year2012, 
    SUM(CASE WHEN posmonth like '2011%' THEN ext ELSE NULL END) AS year2011, 
    SUM(CASE WHEN posmonth like '2010%' THEN ext ELSE NULL END) AS year2010 
FROM combined c 
LEFT JOIN customermaster cm ON c.alias=cm.customerkey 
LEFT JOIN `areamaster` as a ON a.area = c.alias
WHERE cm.company LIKE 'A%' 
GROUP BY customerkey 
ORDER BY cm.company ASC 
LIMIT 0, 50

已编辑再试一次

【讨论】:

感谢您的回复,该编辑给了我一个 SQL 语法错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 9 行的“FROM areamaster ON areakey = c.alias WHERE cm.company LIKE 'A%' GROUP BY cust”附近使用正确的语法 Raheel,我发现了我的问题,我是从错误的位置加入的,不过感谢您所做的一切。

以上是关于在 PHP While 循环中使用 MySQL JOIN 而不是查询的主要内容,如果未能解决你的问题,请参考以下文章

PHP / MYSQL:如何在while循环中循环来自查询的匹配结果

在 while 循环中使用 mysqli_fetch_array 时,PHP/MySQL 如何知道获取下一行?

PHP / MySQL:如何使用WHILE循环插入数据库[重复]

用php中的while编写九九乘法表

将 MySQL While 循环从 PHP 转换为 NodeJS?

PHP第二个while循环在while循环中只运行一次