【问题标题】:Select where *every* record in join contains string选择连接中的*每条*记录包含字符串的位置
【发布时间】:2018-10-08 03:24:32
【问题描述】:

我有以下 MySQL 表:

n_companies:

id 公司名称
1 公司 A
2 B公司

n_contacts:

id company_id 联系人姓名
1 1约翰
2 1 弗兰克
3 2 鲍比
4 2 苏菲

n_custom:

id custom_type custom_type_id 性别 fav_colour 1 触点 1 公红色 2 触点 2 公 红色 3触点3公红 4触点4母绿色

我正在创建一个搜索 UI,允许人们搜索任何/每个/没有记录包含字符串的位置。

这是我认为应该为之工作的查询:

查找每个联系人 (n_contacts) 都是男性的公司 (n_companies)(存储在自定义字段表 n_custom 中):

选择 `n_companies`.`id`, `n_companies`.`company_name` 来自`n_companies` 左连接`n_contacts` 开(`n_contacts`.`company_id` = `n_companies`.`id`) 左连接`n_custom`作为`custom_contacts` 开(`n_contacts`.`id` = `custom_contacts`.`custom_type_id` AND `custom_contacts`.`custom_type` = 'contacts' ) 在哪里 存在( 选择`id` 来自`n_custom` WHERE `n_custom`.`custom_type_id` = `n_contacts`.`id` AND `n_custom`.`custom_type` = 'contacts' AND `n_custom`.`gender` LIKE '%male%' ) 不存在( 选择`id` 来自`n_custom` WHERE `n_custom`.`custom_type_id` = `n_contacts`.`id` AND `n_custom`.`custom_type` = 'contacts' AND `n_custom`.`gender` NOT LIKE '%male%' ) 通过...分组 `n_companies`.`id` 订购方式 `n_companies`.`company_name` ASC

我正在寻找仅返回 Company A 的上述查询,因为它的两个联系人都是男性。 Company B有1男1女。

注意事项:

  • n_companiesc_contacts 是默认创建的表 标准字段。 n_custom 是用户可以创建自己的地方 用于存储各种公司相关表格信息的字段。
  • 我无法在 JOIN 中进行此搜索,因为可能还有另一个 OR 在独立于上述查询的 n_contacts 上搜索。

谁能帮我解释为什么我的查询不起作用?

提前谢谢你。

编辑 我刚刚意识到我的“不”记录也不起作用。在我看来,这应该返回零行,但它不会:

选择 `n_companies`.`id`, `n_companies`.`company_name` 来自`n_companies` 左连接`n_contacts` 开(`n_contacts`.`company_id` = `n_companies`.`id`) 左连接`n_custom`作为`custom_contacts` 开(`n_contacts`.`id` = `custom_contacts`.`custom_type_id` AND `custom_contacts`.`custom_type` = 'contacts' ) 在哪里 不存在 ( 选择`id` 来自`n_custom` WHERE `n_custom`.`custom_type_id` = `n_contacts`.`id` AND `n_custom`.`custom_type` = 'contacts' AND `n_custom`.`fav_colour` NOT LIKE '%red%' ) 通过...分组 `n_companies`.`id` 订购方式 `n_companies`.`company_name` ASC

【问题讨论】:

  • 无余关系除法。
  • 问题似乎是%male% 也包含female
  • 同意,但是如果我们把“男”改成“红”,“女”改成“绿”,还是不行。

标签: mysql sql


【解决方案1】:

您正在寻找联系人符合特定条件的公司。汇总每个公司的标准并查看HAVING 子句中的结果。一个例子:

select *
from companies
where id in
(
  select company_id
  from n_contacts con
  join n_custom cus on cus.custom_type_id = con.id and cus.custom_type = 'contacts'
  group by company_id
  having sum(cus.gender = 'male') = count(*) -- all contacts are male
     and sum(cus.fav_colour = 'red') = 2 -- at least two contacts like red
     and sum(cus.fav_colour = 'green') = 0 -- no contact likes green
     and sum(con.contact_name = 'John') > 0 -- at least one contact is named John
);

【讨论】:

    【解决方案2】:

    您的查询似乎很复杂。怎么样:

    SELECT c.id, c.company_name
    FROM n_companies c LEFT JOIN
         n_contacts co
         ON  co.company_id = c.id LEFT JOIN
         n_custom cu
         ON co.id = cu.custom_type_id AND
            cu.custom_type = 'contacts'
    GROUP BY c.id, c.company_name
    HAVING MIN(cu.gender) = 'male' AND MIN(cu.gender) = MAX(cu.gender);
    

    【讨论】:

    • 谢谢 Gordon,这对我的示例确实有效,但问题是我需要能够按多个条件搜索并包含和/或。因此,在您的查询中,我认为我无法搜索所有名为“Sophie”的联系人,其中每个人都是男性(应该返回公司 A 公司乙...
    猜你喜欢
    • 1970-01-01
    • 2021-01-30
    • 2014-07-15
    • 2017-12-31
    • 1970-01-01
    • 2012-07-16
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多