SELECT bqk.id,bqk.cn_name as cnName,bqk.recorder,bqk.record_time as recordTime
,GROUP_CONCAT(rel_tab.textbooks_id) as relateTextbookString
,GROUP_CONCAT(rel_tab.address) as address
,TRIM(TRAILING '|' FROM GROUP_CONCAT(rel_tab.textbooks_page,'|')) as relatePageString
,TRIM(TRAILING '|' FROM GROUP_CONCAT(rel_tab.textbooks_page_name,'|')) as relatePageNameString
,TRIM(TRAILING '|' FROM GROUP_CONCAT(rel_tab.kp_mistakes_id,'|')) as relateMistakesIdString
FROM question_knowledge bqk
LEFT JOIN (
SELECT * FROM
(
SELECT dis_page_tab.id,dis_base_tk.textbooks_id,dtxt.address,group_concat(dis_base_tk.textbooks_page) as textbooks_page,group_concat(dis_base_tk.textbooks_page_name) as textbooks_page_name,group_concat(dis_base_tk.kp_mistakes_id) as kp_mistakes_id
FROM (
SELECT dis_qk.id, dis_tk.textbooks_page as relatePageString
FROM question_knowledge dis_qk,
textbooks_knowledge dis_tk
WHERE dis_tk.qknowledge_id = dis_qk.id
AND dis_qk.id = 1168
GROUP BY dis_tk.textbooks_page
HAVING COUNT(dis_tk.textbooks_page) = 1
) dis_page_tab
,textbooks_knowledge dis_base_tk
LEFT JOIN textbooks dtxt ON dis_base_tk.textbooks_id = dtxt.id
WHERE dis_page_tab.id = dis_base_tk.qknowledge_id
AND dis_page_tab.relatePageString = dis_base_tk.textbooks_page
GROUP BY dis_base_tk.textbooks_id
) tb1
UNION ALL (
SELECT same_page_tab.id,same_base_tk.textbooks_id,stxt.address,same_base_tk.textbooks_page as textbooks_page_name,same_base_tk.textbooks_page_name as textbooks_page_name,same_base_tk.kp_mistakes_id as kp_mistakes_id
FROM (
SELECT dis_qk.id, dis_tk.textbooks_page as relatePageString
FROM question_knowledge dis_qk,
textbooks_knowledge dis_tk
WHERE dis_tk.qknowledge_id = dis_qk.id
AND dis_qk.id = 1168
GROUP BY dis_tk.textbooks_page
HAVING COUNT(dis_tk.textbooks_page) > 1
) same_page_tab
,textbooks_knowledge same_base_tk
LEFT JOIN textbooks stxt ON same_base_tk.textbooks_id = stxt.id
WHERE same_page_tab.id = same_base_tk.qknowledge_id
AND same_page_tab.relatePageString = same_base_tk.textbooks_page
)
) rel_tab
ON bqk.id = rel_tab.id
WHERE bqk.state=1 AND bqk.id=1168
GROUP BY bqk.id
;