mysql优化咨询加入

时间:2016-09-26 16:26:01

标签: mysql join optimization

任何人都可以帮助我...... 我正在学习ajax,php和mysql。我试图对多个表进行查询,根据品牌形成价格表,进行更新。

我做了什么,这是一个未经优化的查询,这就是我想要的,我并帮助我解释使用"加入"。

结构:

Table 1
+------------+-----------+------------+------------+------------+
|    ID      |  Pro_Cod  |  ActProf   |     mark   |  status    |
+------------+-----------+------------+------------+------------+
|   1        |  00003    |       1    |  002       |      1     |
+------------+-----------+------------+------------+------------+
|   2        |  00033    |       1    |  002       |      1     |
+------------+-----------+------------+------------+------------+
|   3        |  00034    |       2    |  023       |      1     |
+------------+-----------+------------+------------+------------+
|   4        |  00036    |       2    |  009       |      0     |
+------------+-----------+------------+------------+------------+

Table 2
+------------+----------+------------+------------+
|    ID      |  Pro_Cod |  ActProf   |  Name      |
+------------+----------+------------+------------+
|   1        |  00003   |       1    |  product 1 |
+------------+----------+------------+------------+
|   2        |  00033   |       1    |  product 2 |
+------------+----------+------------+------------+
|   3        |  00034   |       2    |  product 3 |
+------------+----------+------------+------------+
|   4        |  00036   |       2    |  product 4 |
+------------+----------+------------+------------+

Table 3
+------------+----------+------------+------------+
|       ID   |  Pro_Cod |  ActProf   |  Price     |
+------------+----------+------------+------------+
|       1    |  00003   |       1    |  10.00     |
+------------+----------+------------+------------+
|       2    |  00033   |       1    |  15.00     |
+------------+----------+------------+------------+
|       3    |  00034   |       2    |  13.00     |
+------------+----------+------------+------------+
|       4    |  00036   |       2    |  14.00     |
+------------+----------+------------+------------+

代码: 说明:表1过滤活跃的产品(状态= 1),并且属于单一品牌(mark = $ var),从表1过滤器的结果中寻找并获得表2和3的数据。

打印数据 产品代码,名称,价格和输入更新。

$ListArray  ='';
        $c=1;#Counter
        #Query 
        $stmtpre    =   "SELECT  Prod_Code , Prod_PF FROM  Avipac_MasterProducts  WHERE Prod_ST=0 AND Prod_Mark='".$s_list_100."';";
        #Consult
        $data       =   $this   ->  DBMANAGER   ->  BDquery($stmtpre, DBPRODUCT);
        #Cicle + Array
        while ($row1    =   mysqli_fetch_assoc($data)){
            #more need data:
            $stmtpre    =   "SELECT   Prod_NameL  FROM  Avipac_Prod_Profile WHERE Prod_Code=".$row1['Prod_Code']." AND Prod_PF='".$row1['Prod_PF']."';";
            $data1      =   $this   ->  DBMANAGER   ->  BDquery($stmtpre, DBPRODUCT);
            $datar      =   $this   ->  DBMANAGER   ->  BDassoc($data1);

            $stmtpre    =   "SELECT  Prod_D_5 FROM  Avipac_Prod_Det_Cost WHERE Prod_Code=".$row1['Prod_Code']." AND Prod_PF='".$row1['Prod_PF']."';";
            $data2      =   $this   ->  DBMANAGER   ->  BDquery($stmtpre, DBPRODUCT);
            $datap      =   $this   ->  DBMANAGER   ->  BDassoc($data2);

            $ListArray  .='<div class="col-md-2 has-warning"><input type="text" class="form-control" name="i_txt_1_'.$c.'" value="'.$row1['Prod_Code'].'" required readonly></div>';
            $c++;
            $ListArray  .='<div class="col-md-6 has-warning"><input type="text" class="form-control" name="i_txt_2_'.$c.'" value="'.$datar['Prod_NameL'].'" required readonly></div>';
            $c++;
            $ListArray  .='<div class="col-md-2 has-warning"><input type="text" class="form-control" name="i_txt_3_'.$c.'" value="'.$datap['Prod_D_5'].'" required readonly></div>';
            $c++;
            $ListArray  .='<div class="col-md-2 has-warning"><input type="text" class="form-control" name="i_txt_4_'.$c.'" value="'.$datap['Prod_D_5'].'"></div>';
            $c=1;
        }
        return $ListArray;

0 个答案:

没有答案