PHP / MYSQL - 数据检索

msed 发布于 2019-07-12 mysql 最后更新 2019-07-12 09:32 4 浏览

通过join比嵌套查询更好的建议,我已将所有嵌套查询转换为join。但是,在转换为join时,我无法从SQL结果中将数据检索到我的数组中。 这是我的疑问: 没有加入

$a="SELECT F_DATE, COUNT(F_DATE) as COUNT_F 
    from FWH 
    where FI_NAME IN 
    ( 
       SELECT I_NAME from INS_W WHERE INSTANCE_ID IN 
       (
         SELECT I_MAP_ID FROM T_MAP where T_MAP_ID = 
         (
           SELECT T_ID FROM TWY WHERE T_NAME = 'abc'
          )
        )
    ) 
    AND F_DATE between '$S_D' AND '$E_D'
    GROUP BY F_DATE";
加入
$a="SELECT t1.F_DATE AS DATE_F, COUNT(t1.F_DATE) as COUNT_F
    from FWH t1 
    JOIN INS_W t2 ON(t1.FI_NAME = t2.I_NAME) 
    JOIN T_MAP t3 ON(t2.INSTANCE_ID = t3.I_MAP_ID) 
    JOIN TWY t4 ON(t3.T_MAP_ID = t4.T_ID) 
    WHERE t4.T_NAME = 'abc' AND
    t1.F_DATE BETWEEN '$S_D' AND 'E_D'GROUP BY t1.F_DATE";
这里是检索数据的PHP代码
$link = mysql_connect("ip", "user", "passs");
$dbcheck = mysql_select_db("db");   
if ($dbcheck) {
    $chart_array_1[] = "['F DATE','F COUNT']";
    $result = mysql_query($a);
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
            $f_date=$row["DATE_F"];
            $f_count=$row["COUNT_F"];
            $chart_array_1[]="['".$f_date."',".$f_count."]";
        }
    }
}
mysqli_close($link);
直接在MySQL DB上进行测试时,SQL查询本身运行良好。
已邀请:

nomnis

赞同来自:

出于某种原因,当我使用连接时,我被迫使用row [0],row [1]等而不是使用列名来获取值。我不明白这背后的原因。但是,这是我的唯一出路。以下代码适用于那些可能陷入与我类似情况的人。

$link = mysql_connect("ip", "user", "passs");
$dbcheck = mysql_select_db("db");   
if ($dbcheck) {
    $chart_array_1[] = "['F DATE','F COUNT']";
    $result = mysql_query($a);
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
            $chart_array_1[]="['".$row[0]."',".$row[1]."]";
        }
    }
}
mysqli_close($link);