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/

相关文章:

sql查询-分组然后加入

java - JDBCTemplate 给出空指针异常?

mysql - 给定单个字段中的内容,如何执行在同一行中的不同字段中传递值的查询?

python - 在多线程 Python 应用程序中最小化 MySQL 连接开销的正确方法是什么?

MySQL : How to remove NOT NULL constraints from all the tables in a given schema

mysql - 选择唯一值和关联的时间戳,而不具有使事物唯一的时间戳

php - 从 MySQL 表的一列获取字符串值并更新同一行的其他列值

sql - 提交表单时, azure 上的 Umbraco 轮廓失败

sql - 如何在 PostgreSQL 中以正确的顺序返回记录

join - webdis 服务器端加入