循环查询具有不同的值

时间:2014-11-23 09:54:43

标签: php mysql

我有一个包含schedules的表sched_id, sc_id1, sc_id2, sc_id3, sc_id4, sc_id5, sc_id6, sc_id7, sc_id8, sc_id9, sc_id10, sched_name

我还有subject_current sc_id, sl_id, schoolyear, semister, etc...表格sc_id1 - scid10是"外键"来自表格sc_id的{​​{1}}

另外,我有subject_currentsubject_list。表sl_id, subject_code, subject_description, subject_prereq中的sl_id是"外键"来自subject_current sl_id的{​​{1}}。

现在,我想做的是回声"来自表格table的{​​{1}}仅向我提供表格subject_listsubject_description的值。

此代码无效:

subject_list

修改

这就是我想要完全发生的事情:

sc_id1 - sc_id10

这是写10次的痛苦。所以我想循环它。但是有人告诉我这很糟糕并且告诉了我schedules。但我怎样才能使用for($jaa = 1;$jaa < 11;$jaa++){ $s_scid = "s_scid".$jaa; $s_sublist = mysql_query("SELECT * FROM subject_current WHERE sc_id='$s_scid'"); while($rows_ss = mysql_fetch_assoc($s_sublist)){ $ss_slid = $rows_ss['sl_id']; $ssl_sublist = mysql_query("SELECT * FROM subject_list WHERE sl_id='$ss_slid'"); while($rows_ssl = mysql_fetch_assoc($ssl_sublist)){ $ssl_slid = $rows_ssl['sl_id']; $ssl_subdesc = $rows_ssl['subject_description']; } } echo $ssl_subdesc; }

1 个答案:

答案 0 :(得分:0)

您描述它的方式,您应该执行以下操作:

for($jaa = 1;$jaa < 11;$jaa++){
    $s_scid = "s_scid".$jaa;
    $the_query = "SELECT sl_id, subject_description FROM subject_list sl JOIN subject_current sc ON sl.sl_id=sc.$s_scid";
    /* the above should generate a JOIN with a particular element from your master table */
    $ssl_sublist = mysql_query($the_query);
    while($rows_ssl = mysql_fetch_assoc($ssl_sublist)){
        $ssl_slid = $rows_ssl['sl_id'];
        $ssl_subdesc = $rows_ssl['subject_description'];
        echo $ssl_subdesc;
    }
}