我知道aw_id,ad_id和grp_id的值(例如aw_id = 5,ad_id = 46和grp_id = 2)。
我想在其中显示所有at_cub_details:
at_cub_details.grp_id = 2 AND
at_cub_details.cd_id = at_cub_awards.cd_id AND
at_cub_awards.aw_id = 5 AND
at_cub_awards.ca_awarded_date IS NULL
OR at_cub_details.cd_id = at_cub_awards.cd_id AND
at_cub_awards.aw_id = 5 does not exist
Where at_cub_details.cd_id = at_cub_awards.cd_id AND
at_cub_awards.aw_id = 5 AND
at_cub_awards.ca_awarded_date = NULL does exist **then**
at_cub_awards.ca_id = at_cub_award_date.ca_id
AND at_cub_award_date.ad_id = 46
AND at_cub_award_date.cad_task_completion_date IS NULL OR
at_cub_awards.ca_id = at_cub_award_date.ca_id AND
at_cub_award_date.ad_id = 46 does not exist
我已经尝试过各种形式的RIGHT和LEFT连接,但是没有任何运气。
最佳答案
解决方案是:
String selectQry = ("SELECT * from ( " +
"SELECT DISTINCT at_cub_details.cd_id as cdid, " +
"at_cub_details.grp_id as grpid, " +
"at_cub_details.cd_surname as surname, " +
"at_cub_details.cd_first_name as firstName, " +
"at_cub_details.cd_dob as dob, " +
"at_cub_details.cd_photograph as photograph, " +
"at_cub_details.cd_archived as archived, " +
"at_cub_details.cd_scout_no as scoutNo " +
"FROM at_account_group, at_cub_details " +
"LEFT JOIN at_cub_awards ON (at_cub_details.cd_id = at_cub_awards.cd_id AND at_cub_awards.aw_id = ?) " +
"WHERE at_cub_awards.cd_id IS NULL " +
"AND at_cub_details.grp_id = at_account_group.grp_id " +
"AND at_account_group.acc_id = ? " +
"UNION " +
"SELECT DISTINCT at_cub_details.cd_id as cdid, " +
"at_cub_details.grp_id as grpid, " +
"at_cub_details.cd_surname as surname, " +
"at_cub_details.cd_first_name as firstName, " +
"at_cub_details.cd_dob as dob, " +
"at_cub_details.cd_photograph as photograph, " +
"at_cub_details.cd_archived as archived, " +
"at_cub_details.cd_scout_no as scoutNo " +
"FROM at_account_group, at_cub_details, at_cub_awards, at_cub_award_date " +
"WHERE at_cub_details.grp_id = at_account_group.grp_id " +
"AND at_account_group.acc_id = ? " +
"AND at_cub_awards.cd_id = at_cub_details.cd_id " +
"AND (at_cub_awards.aw_id = ? AND at_cub_awards.ca_awarded_date IS NULL) " +
"AND (at_cub_awards.ca_id = at_cub_award_date. ca_id " +
"AND at_cub_award_date.cad_task_completion_date IS NULL " +
"AND at_cub_award_date.ad_id = ?) " +
"UNION " +
"SELECT DISTINCT at_cub_details.cd_id as cdid, " +
"at_cub_details.grp_id as grpid, " +
"at_cub_details.cd_surname as surname, " +
"at_cub_details.cd_first_name as firstName, " +
"at_cub_details.cd_dob as dob, " +
"at_cub_details.cd_photograph as photograph, " +
"at_cub_details.cd_archived as archived, " +
"at_cub_details.cd_scout_no as scoutNo " +
"FROM at_account_group, at_cub_details, at_cub_awards " +
"LEFT JOIN at_cub_award_date ON (at_cub_awards.ca_id = at_cub_award_date.ca_id) " +
"WHERE at_cub_award_date.ca_id IS NULL " +
"AND at_cub_awards.ca_awarded_date IS NULL AND at_cub_awards.aw_id = ? " +
"AND at_cub_details.grp_id = at_account_group.grp_id " +
"AND at_account_group.acc_id = ? " +
"AND at_cub_awards.cd_id = at_cub_details.cd_id " +
" ) a " +
"ORDER BY surname, firstName;");
关于mysql - MySQL-如何使用左右联接获取结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35643667/