MySQL,PHP通过比较多个表

时间:2018-06-11 20:43:50

标签: php mysql

我的数据库中有多个表,每天都会更新

我正在做一个网络应用程序,它比较来自多个表格的数据。

数据每天都在更新,所以我真的不知道最大的值,我也不能将它们硬编码到数据库中。

相反,我应该将数据与相应的密钥进行比较。

为了找到我的桌子的头部,我已经进行了比较以找到不同的引擎 - 传输 - 组合并打印其中一个:

## 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"];
      }
    }
  }
}

然后,我回应那些引擎的类别 - 传输 - 组合:

##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>";
  }
}

另外,我打印另一个函数中每列的数据:

打印数据

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>";
  }
}

我想要的是: 虽然我正在回应价值观,但我想以最大的相应价值回应它们,如下:

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

我知道MySQL's GREATEST()函数,它看起来适合我的问题,但我无法弄清楚,如何在我的情况下使用它。

0 个答案:

没有答案