从6个表中选择使用JOIN得到了重复值

时间:2015-11-04 12:38:14

标签: php mysql join mysqli

Tables diagrams here

她是选择学生的mySql

$institute = $_POST['institute'];
$sections = $_POST['sections'];
$division = $_POST['division'];

$getSearch = $db->prepare("SELECT
a.name, a.phase, a.setNumber, a.email, a.sudImage, a.activity, a.id AS stud_id,
b.id, b.ins_name,
c.id, c.sec_name,
d.id, d.div_name,
e.id, e.std_id, e.sub_id, e.absence,
f.id, f.sub_name, f.subHour, f.level
FROM student_basic_info AS a
CROSS JOIN institutes AS b ON (a.institute = b.id)
CROSS JOIN ins_sections AS c ON (a.section = c.id)
CROSS JOIN ins_division AS d ON (a.division = d.id)
CROSS JOIN student_absence AS e
CROSS JOIN ins_subjects AS f ON (e.sub_id = f.id)
WHERE a.institute =? AND a.section = ? AND a.division =?
GROUP BY a.id
");

$studSearch = array();
$getSearch->bind_param('iii', $institute, $sections, $division);
if ($getSearch->execute()) {
$results = $getSearch->get_result();
//var_dump($studSearch);
$row_cnt = $results->num_rows;
?>

问题 结果应该有2行影响
第一排是右边第二排是重复的

见var_dump一行

array (size=1)
  0 => 
    array (size=17)
      'name' => string 'Yousef' (length=6)
      'phase' => string 'اعدادي' (length=12)
      'setNumber' => int 1234
      'email' => string 'johnef' (length=6)
      'sudImage' => string '' (length=0)
      'activity' => int 0
      'stud_id' => int 2
      'id' => int 3
      'ins_name' => string 'fonon' (length=5)
      'sec_name' => string 'فنون مسرح' (length=17)
      'div_name' => string 'شعبة مرجانية 10' (length=26)
      'std_id' => int 4
      'sub_id' => int 3
      'absence' => string '1' (length=1)
      'sub_name' => string 'فنون تطبيقية' (length=23)
      'subHour' => string '2' (length=1)
      'level' => string 'المستوي الأول' (length=25)

见var_dump row two

array (size=2)
  0 => 
    array (size=17)
      'name' => string 'Yousef' (length=6)
      'phase' => string 'اعدادي' (length=12)
      'setNumber' => int 1234
      'email' => string 'johnef' (length=6)
      'sudImage' => string '' (length=0)
      'activity' => int 0
      'stud_id' => int 2
      'id' => int 3
      'ins_name' => string 'fonon' (length=5)
      'sec_name' => string 'فنون مسرح' (length=17)
      'div_name' => string 'شعبة مرجانية 10' (length=26)
      'std_id' => int 4
      'sub_id' => int 3
      'absence' => string '1' (length=1)
      'sub_name' => string 'فنون تطبيقية' (length=23)
      'subHour' => string '2' (length=1)
      'level' => string 'المستوي الأول' (length=25)
  1 => 
    array (size=17)
      'name' => string 'Rida Ali' (length=8)
      'phase' => string 'ابتدائي' (length=14)
      'setNumber' => int 0
      'email' => string 'johnef' (length=6)
      'sudImage' => string '' (length=0)
      'activity' => int 0
      'stud_id' => int 7
      'id' => int 3
      'ins_name' => string 'fonon' (length=5)
      'sec_name' => string 'فنون مسرح' (length=17)
      'div_name' => string 'شعبة مرجانية 10' (length=26)
      'std_id' => int 4
      'sub_id' => int 3
      'absence' => string '1' (length=1)
      'sub_name' => string 'فنون تطبيقية' (length=23)
      'subHour' => string '2' (length=1)
      'level' => string 'المستوي الأول' (length=25)

我尝试来设置这个条件CROSS JOIN student_absence AS e ON (e.std_id = a.id)但是只有一行才出现

使用INNER JOIN尝试2

.....<br/>
FROM student_basic_info AS a
INNER JOIN institutes AS b ON (a.institute = b.id)<br/>
.....

结果
它出现正确它应该出现2行,现在它只出现一行。

使用LEFT JOIN尝试2

    SELECT
a.name, a.phase, a.setNumber, a.email, a.sudImage, a.activity, a.id AS stud_id,
b.id, b.ins_name,
c.id, c.sec_name,
d.id, d.div_name,
e.id, e.std_id, e.sub_id, e.absence,
f.id, f.sub_name, f.subHour, f.level
FROM student_basic_info AS a
INNER JOIN institutes AS b ON (a.institute = b.id)
INNER JOIN ins_sections AS c ON (a.section = c.id)
INNER JOIN ins_division AS d ON (a.division = d.id)

LEFT JOIN student_absence AS e ON (e.std_id = a.id)
LEFT JOIN ins_subjects AS f ON (f.id = e.sub_id)

WHERE a.institute =? AND a.section = ? AND a.division =?
GROUP BY a.id

结果
它出来是正确的

0 个答案:

没有答案