gpt4 book ai didi

MySQL - 如何使用左连接和右连接来获取结果

转载 作者:行者123 更新时间:2023-11-29 21:22:49 26 4
gpt4 key购买 nike

我知道 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 - 如何使用左连接和右连接来获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35643667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com