douchang8219 2018-06-11 20:43
浏览 25

too long

I have multiple tables in my database and those are updated daily

I'm doing a web app, which compares data from multiple tables.

Data is updated daily, so I really don't know the greatest values and I can't hard code those to a database.

Instead, I should compare data with the corresponding key.

To find the head for my table, I already do a comparison to find different Engine - Transmission -combinations and print one of each:

## GET cars with different motors by type
function findEngines($link, $type, $queryNumber, $data)
{
  $query = "
SELECT Engine
     , Transmission
     , ID 
  FROM `model_" . $type . "` 
 GROUP 
    BY Engine
     , Transmission 
 LIMIT " . $queryNumber . ", 1";
  if ($result = mysqli_query($conn, $query)) {
    while ($row = mysqli_fetch_array($result)) {
      //currently, carData is all, which is needed
      if ($data == "engine") {
        return $row["Engine"] . " " . $row["Transmission"];
      } else if ($data == "carData") {
        return $row["ID"];
      }
    }
  }
}

Then, I echo categories for those Engine - Transmission -Combinations:

##Categories
function printCategories($conn, $carID)
{
  $query = "
SELECT TechSpecCategoryName
     , Spec_Name
     , SorterI 
  FROM `categories_" . $carID . "`
 ORDER 
    BY FIELD(`categories_" . $carID . "`.TechSpecCategoryName, 'header1', 'header2', 'header3', 'header4', 'header5', 'header6', 'header7'), `categories_" . $carID . "`.SorterI";

  if ($result = mysqli_query($conn, $query)) {

    $techSpecCategory = "";
    while ($row = mysqli_fetch_array($result)) {
      if ($row['Spec_Name'] == "") {
        $spec_name = "-";
      } else {
        if ($row["TechSpecCategoryName"] != $techSpecCategory) {
          echo "<td class='ad-td w-40'><h5>" . $row["TechSpecCategoryName"] . "</h5></td>";
          $techSpecCategory = $row["TechSpecCategoryName"];

        }
        $spec_name = $row["Spec_Name"];
        $spec_name = preg_replace("/(\/generaattori)/", " ", $spec_name);
      }
      echo "<td class='ad-td w-40'>" . $spec_name . "</td>";

    }
  } else {
    echo mysqli_errno($conn) . ": " . mysqli_error($conn) . "<br>";
  }
}

Also, I print the data for every column in another function:

Print data

function printData($conn, $itemID, $type, $countResults)
{
  if($countResults == 1){
    $tableColWidth = "w-60"; 
  }
  elseif($countResults == 2){
    $tableColWidth = "w-30";
  }
  elseif ($countResults == 3) {
    $tableColWidth = "w-20";  
  }
  else {
    $tableColWidth = "w-15";  
  }

  $table = str_replace("-", "_", $itemID);

  $queryCarData = "SELECT `categories_" . $type . "`.Item_Name, `categories_" . $type . "`.SorterI, `" . $table . "`.*, COALESCE(SUBSTRING(`" . $table . "`.Item_value, 1, 15), '-') FROM `categories_" . $type . "`
  LEFT JOIN  `" . $table . "`
  ON `" . $table . "`.Item_Name = `categories_" . $type . "`.Item_Name
  ORDER BY FIELD(`categories_" . $type . "`.TechSpecCategoryName, 'Kulutus ja päästöt', 'Moottori', 'Vaihteisto', 'Suorituskyky', 'Jarrut', 'Jousitus', 'Ohjaus', 'Mitat ja massat', 'Kuormauskapasiteetti', 'Offroad', 'Renkaat ja vanteet'), `categories_" . $type . "`.SorterI";

  if ($result = mysqli_query($conn, $queryCarData)) {
    $rowDataNumber = 0;
    $techspecCategory = "";
    while ($row = mysqli_fetch_array($result)) {

      if ($row['Item_Name'] == "") {
        $Item_value = "-";
      } else {

        if ($row["TechSpecCategoryName"] != $techspecCategory) {
          echo "<td class='ad-td ".$tableColWidth."'>" . "<br>" . "</td>";
          $Item_value = " ";
          $techspecCategory = $row["TechSpecCategoryName"];
          $rowDataNumber++;
        }
        $Item_value = $row["Item_value"];
      }
      echo "<td class='ad-td ".$tableColWidth."'>" . $Item_value . "</td>";
      $rowDataNumber++;
    }
  } else {
    echo mysqli_errno($conn) . ": " . mysqli_error($conn) . "<br>";
  }
}

What I'd like to archieve: while i'm echoing values, I'd like to echo them with greatest corresponding value, like this:

echo "<td class='ad-td ".$tableColWidth."'>" . $Item_value ." / ". $greatest_value ."</td>";

I'm aware of MySQL's GREATEST() function, which looks like right for my problem, but I couldn't figure out, how to use it in a situation as mine is.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看