我有这个有效的SQL查询:
SELECT * from ( SELECT distinct ts.users_user_id
from subjects s LEFT JOIN teachers_subjects ts
ON s.subject_ID = ts.subject_id
JOIN users_teachers ut
ON ut.user_id = ts.users_user_id
LEFT JOIN stations st
ON st.station_id = ut.station_id
LEFT JOIN majors mj
ON mj.major_id = ut.major_id
".$where." ) qualified
JOIN users u ON qualified.users_user_id = u.user_id
JOIN users_teachers ut ON qualified.users_user_id = ut.user_id
JOIN teachers_subjects ts2 ON qualified.users_user_id = ts2.users_user_id
JOIN subjects s2 ON ts2.subject_id = s2.subject_id
LEFT JOIN stations st ON ut.station_id = st.station_id"
这是为了a)能够匹配某些搜索条件和b)允许我为每个结果输出多个subject_id,因为每个结果代表一个教授多个主题的教师,这就是为什么有两个连接相同的栏目。
我现在必须添加另外两个连接来添加这个混乱。
SELECT * from ( SELECT distinct ts.users_user_id
from subjects s LEFT JOIN teachers_subjects ts
ON s.subject_ID = ts.subject_id
JOIN users_teachers ut
ON ut.user_id = ts.users_user_id
LEFT JOIN stations st
ON st.station_id = ut.station_id
LEFT JOIN majors mj
ON mj.major_id = ut.major_id
LEFT JOIN lessons l
ON l.teacher_id = ut.user_id
LEFT JOIN ratings r
ON r.lesson_id = l.lesson_id
".$where." ) qualified
JOIN users u ON qualified.users_user_id = u.user_id
JOIN users_teachers ut ON qualified.users_user_id = ut.user_id
JOIN teachers_subjects ts2 ON qualified.users_user_id = ts2.users_user_id
JOIN subjects s2 ON ts2.subject_id = s2.subject_id
LEFT JOIN lessons l2 ON l.teacher_id = qualified.users_user_id
LEFT JOIN ratings r2 ON r2.lesson_id = l2.lesson_id
LEFT JOIN stations st ON ut.station_id = st.station_id"
不幸的是,我没有得到所需的输出。 a)没有输出正确的课程ID(只有少数几个应该输出)和b)从课程表派生的数组中的项目数量是它应该的n倍,其中n =数字该老师的科目。所以有很多冗余和很多空数组插槽
非常感谢任何帮助
课程表结构:
CREATE TABLE `lessons` (
`lesson_id` int(11) NOT NULL AUTO_INCREMENT,
`lesson_date` date NOT NULL,
`time_began` time NOT NULL,
`time_ended` time NOT NULL,
`duration` int(11) NOT NULL COMMENT 'minutes',
`total_fee` int(11) NOT NULL,
`student_id` int(11) NOT NULL,
`teacher_id` int(11) NOT NULL,
`request_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`pay_rate` int(11) NOT NULL,
`subject_id` int(11) NOT NULL,
`student_reject` tinyint(4) NOT NULL DEFAULT '0',
`student_reject_comment` text,
`lesson_notes` text,
PRIMARY KEY (`lesson_id`),
KEY `student_id` (`student_id`),
KEY `teacher_id` (`teacher_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;
评级表结构:
CREATE TABLE `ratings` (
`lesson_id` int(11) NOT NULL,
`like_dislike` tinyint(4) NOT NULL,
`review` text NOT NULL,
PRIMARY KEY (`lesson_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
期望的输出:
[rej] => Array
(
[avatar] => 122.JPG
[email] =>
[last_name] => kob
[user_name] => rej1324558654
[english_name] => Array
(
[0] => SAT
[1] => math
)
[station_en_name] =>
[country_prefix] => AR
[pay] => 201
[lesson_id] => Array
(
[0] => 11
[1] => 12
[2] => 13
[3] => 14
)
)
原始输出具有每个主题名称的四倍,而不是正确的课程ID,而不是在lesson_id数组中有许多空白插槽
答案 0 :(得分:0)
这可能不是你想听到的,但我真的强烈建议将这个查询分成多个一口大小,可测试的块。它会添加一些I / O,但您可以使用VIEWS或CTE控制它,具体取决于您的环境。虽然它可能不适合生产运行,但拥有控制总数可以让您首先确定问题的来源。
如果您决定这样做,我将从以下
开始SELECT COUNT(DISTINCT subject_ID) FROM subjects;
SELECT COUNT(DISTINCT subject_id) FROM teachers_subjects;
SELECT count(*)
FROM subjects as s
INNER JOIN teachers_subjects as ts
ON s.subject_ID = ts.subject_id ;
然后向下移动列表并测试所有连接。
测试所有这些工作需要付出很多努力,但无论是在修复现在的问题还是解决以后可能遇到的任何问题,它都可能会得到回报。如果这是一个实时数据环境,你可以合理地预期最终会出现问题...某些数据加载不正确或者其他什么。预先编写控制总数将允许您稍后放大以解决可能出现的任何问题,而不仅仅是您正在开始的那个问题