dpgjci27392 2015-11-04 12:38
浏览 34

使用JOIN从6个表中选择获取重复值

Tables diagrams here

her is the mySql to select the students

$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;
?>

The Problem is the results should have 2 rows effected
row one come's out right the second come's duplicated

see the var_dump row one

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)

see the 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)

I try to put this condition CROSS JOIN student_absence AS e ON (e.std_id = a.id) but it come's out with one row only

try 2 with INNER JOIN

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

results
it coming out correct but it should come out with 2 rows now it's coming out with one row only.

try 2 with LEFT JOIN

    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

results
it coming out correct

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值