0

这是我的实际查询:

SELECT temp.id_company AS id_company
     , temp.code_product AS id_product
     , temp.date_product  AS date_p
FROM (
   SELECT *
   FROM companies 
   INNER JOIN products
      ON products.id_company_product = companies.id_company
   WHERE module_key_company = "daL/RqzZcfqc."
   ) AS temp
WHERE temp.code_product = "app_12"

首先我得到companies.id_company where companies.module_key_company匹配“dal.....”然后我检查我是否得到匹配products.code_product = "app_12"并且products.id_company = companies.id_company我在第一次测试后得到。

如果这两个条件都成立,我就会得到我想要的。但如果没有匹配的products.code_product.

module_key即使错误,我也无法找到仅通过测试才能获得 id_company的code_product方法。

谢谢你的帮助。

4

2 回答 2

1

试试这个:

SELECT companies.id_company
     , coalese(products.code_product,'NONE') as id_product
     , products.date_product as date_p
FROM companies 
LEFT OUTER JOIN products
   ON     products.id_company_product = companies.id_company
      and products.code_product = 'app_12'

WHERE companies.module_key_company = 'daL/RqzZcfqc.'

id_product列将包含文本NONE ,对于没有任何匹配产品的公司,该date_p列将为空。此外,您应该使用单引号来提供字符常量,而不是双引号。

于 2012-11-11T01:22:10.180 回答
0

很难说你的表结构是什么,除非你用表名作为列名的后缀。所以这就是我的猜测。试试这个:

SELECT C.id_company, P.code_product, P.date_product
  FROM Companies AS C
    LEFT OUTER JOIN Products AS P ON P.id_company_product = C.id_company
  WHERE module_key_company = 'daL/RqzZcfqc.'
    AND (P.code_product = 'app_12' OR P.code_product is NULL) 

最好的,乔

于 2012-11-11T01:20:20.137 回答