我的 sqlfiddle
我的数据库架构和在线查询
查询1
SELECT pk_video_master_id AS _id,
video_title AS title1,
COUNT(fk_video_master_id) AS sub_title_1,
SUM(video_detail_length) AS sub_title_2,
-1007 AS module
FROM tbl_video_master
LEFT JOIN tbl_video_detail ON fk_video_master_id = pk_video_master_id
WHERE title1 LIKE '%a%' GROUP BY _id
查询2
SELECT pk_surveys_id AS _id ,
surveys_title AS title1,
surveys_end_date AS sub_title_1,
COUNT(fk_survey_detail_survey_id) AS sub_title_2,
-1006 AS module
FROM tbl_surveys
LEFT JOIN tbl_survey_detail ON fk_survey_detail_survey_id = pk_surveys_id
WHERE title1 LIKE '%a%' GROUP BY _id
我想UNION这2个查询我也创建了
SELECT pk_video_master_id AS _id,
video_title AS title1,COUNT(fk_video_master_id) AS sub_title_1,
SUM(video_detail_length) AS sub_title_2,-1007 AS module
FROM tbl_video_master
LEFT JOIN tbl_video_detail ON fk_video_master_id = pk_video_master_id
UNION
SELECT pk_surveys_id AS _id ,surveys_title AS title1,
surveys_end_date AS sub_title_1,
COUNT(fk_survey_detail_survey_id) AS sub_title_2,
-1006 AS module
FROM tbl_surveys
LEFT JOIN tbl_survey_detail ON fk_survey_detail_survey_id = pk_surveys_id
WHERE title1 LIKE '%a%' GROUP BY module
但它只返回2条记录
我的欲望输出是在表TITLE1字段
中搜索所以请帮帮我
答案 0 :(得分:2)
您从query1中删除了GROUP BY。大多数其他DBMS会引发错误,但SQLite只返回一条带有随机信息的记录。顺便说一下,你也删除了WHERE子句。
将完整的查询1粘贴到完整的查询中2.并使用UNION ALL而不是UNION,因为无论如何都不会删除重复项。
SELECT pk_video_master_id AS _id,
video_title AS title1,COUNT(fk_video_master_id) AS sub_title_1,
SUM(video_detail_length) AS sub_title_2,-1007 AS module
FROM tbl_video_master
LEFT JOIN tbl_video_detail ON fk_video_master_id = pk_video_master_id
WHERE title1 LIKE '%a%' GROUP BY _id
UNION ALL
SELECT pk_surveys_id AS _id ,surveys_title AS title1,
surveys_end_date AS sub_title_1,
COUNT(fk_survey_detail_survey_id) AS sub_title_2,
-1006 AS module
FROM tbl_surveys
LEFT JOIN tbl_survey_detail ON fk_survey_detail_survey_id = pk_surveys_id
WHERE title1 LIKE '%a%' GROUP BY module;
以下是您的SQL文件:http://sqlfiddle.com/#!5/03ecc/14。