所以我对query
db
mysql
SELECT CFM.display_name, CFM.field_type, CFM.option_value
FROM company_mast
LEFT JOIN component_mast ON company_mast.id = component_mast.company_id
LEFT JOIN CustomField_mast CFM ON CFM.Company_ID = Component_mast.Company_ID
AND CFM.Component_ID = component_Mast.Component_ID
WHERE component_mast.component_name = 'Assets'
AND component_mast.project_id = '1'
它会完全返回我想要的内容。我也可以像这样查询
SELECT CFM.display_name, CFM.field_type, CFM.option_value
FROM company_mast
LEFT JOIN component_mast ON company_mast.id = component_mast.company_id
LEFT JOIN CustomField_mast CFM ON CFM.Company_ID = Component_mast.Company_ID
AND CFM.Component_ID = component_Mast.Component_ID
WHERE component_mast.component_name = 'Assets'
AND company_mast.company_name = 'Company' <!-- this line is different -->
它再次完全返回我想要的东西。但是,如果我运行此查询
SELECT CFM.display_name, CFM.field_type, CFM.option_value
FROM company_mast
LEFT JOIN component_mast ON company_mast.id = component_mast.company_id
LEFT JOIN CustomField_mast CFM ON CFM.Company_ID = Component_mast.Company_ID
AND CFM.Component_ID = component_Mast.Component_ID
WHERE component_mast.component_name = 'Assets'
AND component_mast.project_id = '1'
AND company_mast.company_name = 'Company'
我得到0结果。现在,这是有效的,但随着这个数据库的增长,同一公司中将有多个具有相同名称的组件。所以我需要添加一个额外的唯一标识符,即project_id。
为什么第三个查询不返回前两个查询?我该如何解决?
答案 0 :(得分:3)
也许您需要or
条件而不是and
条件:
SELECT CFM.display_name, CFM.field_type, CFM.option_value
FROM company_mast
LEFT JOIN component_mast ON company_mast.id = component_mast.company_id
LEFT JOIN CustomField_mast CFM ON CFM.Company_ID = Component_mast.Company_ID
AND CFM.Component_ID = component_Mast.Component_ID
WHERE component_mast.component_name = 'Assets'
AND (component_mast.project_id = '1' or company_mast.company_name = 'Company')