0

这是我的桌子:

auth_keys

id    /    keys    / user 
1     /    12345   /   1

*产品 * :

id    / company    /   name
1     /  1         /   iphone
2     /  2         /   iphone

公司

id    / name       /   mkey
1     / myComp     /   54321

这是我的 sql 请求:

SELECT 
    products.id_product AS id_product, 
    auth_keys.keys AS vkey,
    auth_keys.user AS user_id
FROM 
    auth_keys
JOIN 
    products
LEFT JOIN 
    companies ON products.company = companies.id
WHERE 
    products.name_product = "iphone"
    AND companies.mkey = "54321" 
HAVING 
    vkey = "none"

我想要的东西:

  • id_products我得到第一个THENid_companyname_product因为我可以为两家不同的公司拥有相同名称的产品。这部分没问题。

  • 我想要user_key价值WHERE keys = "12345" OR "NONE",那是我的问题。如果keys =“12345”一切正常,但如果keys =“none/nothing”我什么也得不到......

我想得到第一个结果,id_product,即使我无法匹配user_key...

谢谢你的帮助 !

4

2 回答 2

0

我真的很难理解这些表是如何连接在一起的。您似乎希望加入 auth_keys 没有任何条件,除了密钥必须是 12345 或 NONE。如果两者都匹配,则不确定您想要什么。

然而快速尝试

SELECT products.id_product AS id_product, 
    auth_keys.keys AS vkey,
    auth_keys.user AS user_id
FROM products
LEFT OUTER JOIN companies
ON products.company = companies.id
AND companies.mkey = "54321"
LEFT OUTER JOIN auth_keys
ON auth_keys.keys IN ('12345', 'NONE')
WHERE products.name_product = "iphone"
于 2013-06-27T16:30:38.640 回答
0

尝试这个:


    SELECT 
    products.id_product AS id_product, 
    auth_keys.keys AS vkey,
    auth_keys.user AS user_id
    FROM auth_keys
    JOIN products
    LEFT JOIN companies ON products.company = companies.id
    WHERE 
    products.name_product = "iphone"
    AND (companies.mkey = "54321"or vkey = "none")

不需要“拥有”,因为它与“分组依据”子句一起使用来过滤聚合结果。

于 2013-06-27T15:40:25.383 回答