mysql - 在 JOIN 上排除 MySQL 中的结果

标签 mysql sql join birt

下面是我的MySQL语句:

select
    tableT.id,
    tableTGroupName,
    tableTGroupSortOrder,
    tableT_group.Deleted as tableTGroupDeleted,
    tableTSuiteName,
    tableTSuiteSortOrder,
    tableT_suite.Deleted as tableTSuiteDeleted,
    tableTCaseName, 
    tableTCaseSortOrder,
    tableT_case.Deleted as tableTCaseDeleted,
    DbPackageName,
    E_imgLR as Eimg, 
    R_imgLR as Rimg,
    tableTResultState,
    tableTResultComment
from 
    tableT_result
    inner join tableT on tableT_id = tableT.id
    inner join tableT_run on tableT_run_id = tableT_run.id
    inner join db_package on db_package_id = db_package.id
    inner join tableT_case on tableT_case_id = tableT_case.id
    inner join tableT_suite on tableT_suite_id = tableT_suite.id
    inner join tableT_group on tableT_group_id = tableT_group.id
    left outer join E_img_lo_r on tableT_case.e_img_id = E_img_lo_r.e_img_id or tableT_suite.e_img_id = E_img_lo_r.e_img_id -- Problematic line
    left outer join result_image_low_res on tableT_result.result_image_id = result_image_low_res.result_image_id
where
    db_package_label_id = ?
order by 
    tableTGroupSortOrder, 
    tableTSuiteSortOrder, 
    tableTCaseSortOrder

最后这个语句给了我 16 行,其中 3 行(结果)是多余的。 3 x 2 行具有相同的 id,例如:(234,234) (71,71) (445,445)。线

left outer join E_img_lo_r on tableT_case.e_img_id = E_img_lo_r.e_img_id or tableT_suite.e_img_id = E_img_lo_r.e_img_id

导致这个问题,我可以使用DISTINCT来消除重复的ID,但是行的内容并不相同。这就是为什么我不想消除必要的行。

我的目的是首先检查“tableT_case”是否有图像,如果有则使用它,如果没有则使用“tableT_suite”中的图像,如果“tableT_suite”也没有图像则采用默认的“无图像” ”。显然 if-else 在 join 语句中是不可能的。当两个表都包含图像时,它会同时获取我不想要的图像。我需要这个用于 Birt 报告,我们不想在报告上显示错误的图像。我很乐意提供任何提示。

最佳答案

你可以只添加一个吗?将有问题的行更改为:

left outer join E_img_lo_r
on
tableT_case.e_img_id = E_img_lo_r.e_img_id
or 
(
    tableT_case.e_img_id != E_img_lo_r.e_img_id
    and
    tableT_suite.e_img_id = E_img_lo_r.e_img_id
)

关于mysql - 在 JOIN 上排除 MySQL 中的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20428693/

相关文章:

MySQL Order By 减慢连接速度

mysql - SQL 中的泛化和分类

mysql - 无法在 FROM 子句中指定要更新的目标表 t4

mysql - 具有多个条件的加入

php - 如何使用 AJAX 和 PHP 从 MySQL 数据库中提取实时用户列表

Mysql 左连接还是更简单的方法?

mysql - 如何将子字符串的最后一个字符替换为取决于另一个字段字符串值的另一个字符

MySQL - 如何将 "Using join buffer (Block Nested Loop)"添加到查询中?

python - 如何匹配两个数据框并检索匹配的行

PostgreSQL 加入 : delete records present in one table, 但不是另一个