我的名为tutor_signup_form
的mysql表看起来像这样
id feerange gender name
1 20 male alex
2 10 female Robin
3 15 male Babu
现在我有一个html搜索表单,其中很少下拉所选字段,并且存在许多Chckbox字段。我想使用sql INNER JOIN
从多个表中获取搜索结果。我的INNER JOIN
查询看起来像这样
// search query
$query = mysql_query("SELECT tutor_signup_form . *, tutor_signup_edu_psle . *,
tutor_signup_edu_olevel . *, tutor_signup_edu_alevel . *, tutor_signup_edu_diploma . *,
tutor_signup_edu_degree . *, tutor_signup_edu_masters . *, tutor_signup_edu_psd . *,
tutor_signup_level_primary . *, tutor_signup_level_olevel . *, tutor_signup_level_alevel . *,
tutor_signup_level_int . *, tutor_signup_level_uni . *, tutor_signup_level_music . *,
tutor_signup_level_lang . *, tutor_signup_level_com . *, tutor_signup_pre_central . *,
tutor_signup_pre_east . *, tutor_signup_pre_west . *, tutor_signup_pre_south . *,
tutor_signup_pre_north . *, tutor_signup_pre_ne . *, tutor_signup_pre_nw . * FROM tutor_signup_form
INNER JOIN tutor_signup_edu_psle ON tutor_signup_form.tutor_id = tutor_signup_form.tutor_id
INNER JOIN tutor_signup_edu_olevel ON tutor_signup_form.tutor_id = tutor_signup_edu_olevel.tutor_id
INNER JOIN tutor_signup_edu_alevel ON tutor_signup_form.tutor_id = tutor_signup_edu_alevel.tutor_id
INNER JOIN tutor_signup_edu_diploma ON tutor_signup_form.tutor_id = tutor_signup_edu_diploma.tutor_id
INNER JOIN tutor_signup_edu_degree ON tutor_signup_form.tutor_id = tutor_signup_edu_degree.tutor_id
INNER JOIN tutor_signup_edu_masters ON tutor_signup_form.tutor_id = tutor_signup_edu_masters.tutor_id
INNER JOIN tutor_signup_edu_psd ON tutor_signup_form.tutor_id = tutor_signup_edu_psd.tutor_id
INNER JOIN tutor_signup_level_primary ON tutor_signup_form.tutor_id = tutor_signup_level_primary.tutor_id
INNER JOIN tutor_signup_level_olevel ON tutor_signup_form.tutor_id = tutor_signup_level_olevel.tutor_id
INNER JOIN tutor_signup_level_alevel ON tutor_signup_form.tutor_id = tutor_signup_level_alevel.tutor_id
INNER JOIN tutor_signup_level_int ON tutor_signup_form.tutor_id = tutor_signup_level_int.tutor_id
INNER JOIN tutor_signup_level_uni ON tutor_signup_form.tutor_id = tutor_signup_level_uni.tutor_id
INNER JOIN tutor_signup_level_music ON tutor_signup_form.tutor_id = tutor_signup_level_music.tutor_id
INNER JOIN tutor_signup_level_lang ON tutor_signup_form.tutor_id = tutor_signup_level_lang.tutor_id
INNER JOIN tutor_signup_level_com ON tutor_signup_form.tutor_id = tutor_signup_level_com.tutor_id
INNER JOIN tutor_signup_pre_central ON tutor_signup_form.tutor_id = tutor_signup_pre_central.tutor_id
INNER JOIN tutor_signup_pre_east ON tutor_signup_form.tutor_id = tutor_signup_pre_east.tutor_id
INNER JOIN tutor_signup_pre_west ON tutor_signup_form.tutor_id = tutor_signup_pre_west.tutor_id
INNER JOIN tutor_signup_pre_south ON tutor_signup_form.tutor_id = tutor_signup_pre_south.tutor_id
INNER JOIN tutor_signup_pre_north ON tutor_signup_form.tutor_id = tutor_signup_pre_north.tutor_id
INNER JOIN tutor_signup_pre_ne ON tutor_signup_form.tutor_id = tutor_signup_pre_ne.tutor_id
INNER JOIN tutor_signup_pre_nw ON tutor_signup_form.tutor_id = tutor_signup_pre_nw.tutor_id
WHERE tutor_signup_form.feerange <= '$budget'");
例如,我选择了预算字段= 20.因此,我的查询应该只是来自tutor_signup_form
的产品3结果,但它会产生24个结果。我不明白它为什么会产生24个结果。你能解决它吗?
更新:
答案 0 :(得分:1)
正如Deadman所说,笛卡儿......
您的第一个内部联接是表名的复制/粘贴
INNER JOIN tutor_signup_edu_psle
ON tutor_signup_form.tutor_id = tutor_signup_form.tutor_id
应该是
INNER JOIN tutor_signup_edu_psle
ON tutor_signup_form.tutor_id = tutor_signup_edu_psle.tutor_id
我没有读过去,但是会在平均时间
此外,您拥有所有联接作为INNER JOIN。如果有人在其中一个表(例如音乐)中没有记录,但是在计算机中进行辅导,并且有人搜索计算机,则不会返回这些记录。我建议所有人都改为LEFT JOIN,所以如果记录存在于辅导子级别,那么你很好,不会失去它。
答案 1 :(得分:0)
如果我做得对,你需要检查你的where子句。我认为参与加入的表的笛卡尔积。 笛卡尔积结果集的大小是第一个表中的行数乘以第二个表中的行数