我有两张桌子 - 类似于下面的表格。第一个表存储有关每个作业的信息:
+--------+------------+
| JOB_ID | JOB_NAME |
+--------+------------+
| | |
| 1 | Test Job 1 |
| | |
| 2 | Test Job 2 |
| | |
| 3 | Test Job 3 |
| | |
| 4 | Test Job 4 |
| | |
| 5 | Test Job 5 |
| | |
| 6 | Test Job 6 |
| | |
| 7 | Test Job 7 |
+--------+------------+
第二张表存储了与每项工作相关的一个或多个“主题”:
+------------+--------+------------------+
| SUBJECT_ID | JOB_ID | SUBJECT_NAME |
+------------+--------+------------------+
| | | |
| 1 | 1 | Test Subject #1 |
| | | |
| 2 | 1 | Test Subject #2 |
| | | |
| 3 | 1 | Test Subject #3 |
| | | |
| 4 | 2 | Test Subject #4 |
| | | |
| 5 | 3 | Test Subject #5 |
| | | |
| 6 | 3 | Test Subject #6 |
| | | |
| 7 | 3 | Test Subject #7 |
| | | |
| 8 | 4 | Test Subject #8 |
| | | |
| 9 | 5 | Test Subject #9 |
| | | |
| 10 | 6 | Test Subject #10 |
| | | |
| 11 | 7 | Test Subject #11 |
| | | |
| 12 | 7 | Test Subject #12 |
| | | |
| 13 | 7 | Test Subject #13 |
+------------+--------+------------------+
目前,我有一个显示所有作业的SQL语句,以及两个字段中的前两个“主题”,结果如下:
+--------+------------+------------------+------------------+
| JOB_ID | JOB_NAME | SUBJECT 1 | SUBJECT 2 |
+--------+------------+------------------+------------------+
| | | | |
| 1 | Test Job 1 | Test Subject #1 | Test Subject #2 |
| | | | |
| 2 | Test Job 2 | Test Subject #4 | |
| | | | |
| 3 | Test Job 3 | Test Subject #5 | Test Subject #6 |
| | | | |
| 4 | Test Job 4 | Test Subject #8 | |
| | | | |
| 5 | Test Job 5 | Test Subject #9 | |
| | | | |
| 6 | Test Job 6 | Test Subject #10 | |
| | | | |
| 7 | Test Job 7 | Test Subject #11 | Test Subject #12 |
+--------+------------+------------------+------------------+
这是由以下SQL生成的:
SELECT job.job_id as 'Id',
job.job_name as 'JobName',
Subjects1.Subject as 'Subject 1',
Subjects2.Subject as 'Subject 2',
LEFT JOIN job_Subjects as Subjects1 ON Subjects1.job_id = job.job_id AND Subjects1.Subject_id = (SELECT Subject_id FROM job_Subjects WHERE job_Subjects.job_id = job.job_id ORDER BY job_Subjects.Subject_index LIMIT 1)
LEFT JOIN job_Subjects as Subjects2 ON Subjects2.job_id = job.job_id AND Subjects2.Subject_id = (SELECT Subject_id FROM job_Subjects WHERE job_Subjects.job_id = job.job_id ORDER BY job_Subjects.Subject_index LIMIT 1,1)
但是,我还需要搜索主题的名称(job_subjects.subject字段)。
我尝试了以下内容:
如何搜索主题表,同时保持两个连接的方式?
答案 0 :(得分:1)
MySQL有一个很好的扩展,您可以使用HAVING
子句来执行您想要的操作。首先,我建议你使用反引号作为列名 - 或使用不需要转义的名称。
SELECT job.job_id as Id, job.job_name as JobName,
Subjects1.Subject as Subject1,
Subjects2.Subject as Subject2
FROM ?? LEFT JOIN
job_Subjects Subjects1
ON Subjects1.job_id = job.job_id AND
Subjects1.Subject_id = (SELECT Subject_id FROM job_Subjects WHERE job_Subjects.job_id = job.job_id ORDER BY job_Subjects.Subject_index LIMIT 1
) LEFT JOIN
job_Subjects Subjects2
ON Subjects2.job_id = job.job_id AND
Subjects2.Subject_id = (SELECT Subject_id FROM job_Subjects WHERE job_Subjects.job_id = job.job_id ORDER BY job_Subjects.Subject_index LIMIT 1,1
)
HAVING Subject1 LIKE '%Test Job 3%';
您也可以使用子查询执行此操作。那将是更传统的方法。但是,MySQL会实现子查询,从而导致性能上的一些开销。