1

我不确定我想要做什么是可能的,但我一直在尝试用不同的方法来解决我需要的解决方案,但到目前为止我空手而归。

假设我有 2 个表(只是一个例子,在我的情况下,还有更多 + 更多数据)

一个称为客户端,另一个称为 form_data。我们在 clients 表中有多个客户,在 form_data 表中,对于 clients 表中存在的每个公司,我们都有多行。在 form_data 我们存储来自不同表单的序列化数据。(身份证和数据)

我目前正在从 form_data 表中提取所有记录,并且我正在尝试在数据列上使用正则表达式来过滤,例如在其中找到值“机油”。

我想要一种方法来进行此过滤,但过滤公司而不是表格..所以我想找到其中包含“机油”的表格,并删除没有此匹配项的 COMPANIES 的所有条目,但是我想保留为匹配的公司显示的所有表格。

我可以发布我的查询,但它相当长,我认为如果我们能解决上述问题,我应该足以实现实际查询。

问候

编辑:

SELECT f.form_question_has_answer_id AS f__form_question_has_answer_id, f.form_question_has_answer_request AS f__form_question_has_answer_request, 
f.form_question_has_answer_form_id AS f__form_question_has_answer_form_id, f.form_question_has_answer_user_id AS f__form_question_has_answer_user_id, 
p.project_company_has_user_id AS p__project_company_has_user_id, p.project_company_has_user_project_id AS p__project_company_has_user_project_id, 
p.project_company_has_user_user_id AS p__project_company_has_user_user_id, c.company_id AS c__company_id, c.company_hall_no AS c__company_hall_no, 
c.company_type AS c__company_type, c.company_company_name AS c__company_company_name, c.company_country AS c__company_country, 
c.company_stand_number AS c__company_stand_number, c.company_image_file_1 AS c__company_image_file_1, p2.project_id AS p2__project_id, 
p2.project_name AS p2__project_name, u.user_id AS u__user_id, u.user_username AS u__user_username, f2.form_id AS f2__form_id 
FROM form_question_has_answer f 
INNER JOIN project_company_has_user p ON f.form_question_has_answer_user_id = p.project_company_has_user_user_id 
INNER JOIN company c ON p.project_company_has_user_company_id = c.company_id 
INNER JOIN project p2 ON p.project_company_has_user_project_id = p2.project_id 
INNER JOIN user u ON p.project_company_has_user_user_id = u.user_id 
INNER JOIN form f2 ON p.project_company_has_user_project_id = f2.form_project_id 
WHERE f.form_question_has_answer_id IN ('19262', '21560', '23088', '22660', '14772', '18495', '18720', '21625', '19957', '20943') 
AND ((f2.form_template_name = "custom" AND p.project_company_has_user_garbage_collection = 0 AND p.project_company_has_user_project_id = 29) AND f.form_question_has_answer_request REGEXP 'item-cadcae') 
ORDER BY company_company_name asc

该查询来自一个教义查询。

编辑:如果我的公司在 form_data 表中有 10 个表格,如果我按“机油”过滤,则该公司没有机油的所有表格都将被删除。所以如果公司 id 144 的 10 个表格中只有 1 个表格有机油这个词,那么其他 9 种形式在查询中丢失了。我想保留它们。我想要的是任何没有找到与该匹配项(机油)匹配的任何表格的公司,将其所有表格从搜索中删除。

4

4 回答 4

1
SELECT
    c.*
FROM 
    clients as c
    INNER JOIN (
        select 
            *
        from 
            forms
        where 
            sometext like '%motor oil%'
        ) as f
        ON f.client = c.id

http://sqlfiddle.com/#!3/5b616/1

在这部分:

select 
    *
from 
      forms
where 
      sometext like '%motor oil%'

您可以使用适当的过滤器放置任何从表单表中选择相关行的查询。

您也可以使用左外连接 where null 技术进行相反的操作。尽管在这种情况下修改子查询以返回不同客户端的列表可能会给您带来更符合您期望的结果:

SELECT
    c.*
FROM 
    clients as c
    LEFT OUTER JOIN (
        select 
            distinct 
                client
        from 
            forms
        where 
            sometext like '%motor oil%'
        ) as f
        ON f.client = c.id
where f.client is null

http://sqlfiddle.com/#!3/5b616/4

因此,您的查询类似于:

SELECT
    c.*
FROM 
    company AS c 
    INNER JOIN (
                SELECT 
                    DISTINCT c.company_id 
                FROM 
                    form_question_has_answer f 
                    INNER JOIN project_company_has_user p 
                        ON f.form_question_has_answer_user_id = p.project_company_has_user_user_id 
                    INNER JOIN company c 
                        ON p.project_company_has_user_company_id = c.company_id 
                    INNER JOIN project p2 
                        ON p.project_company_has_user_project_id = p2.project_id 
                    INNER JOIN user u 
                        ON p.project_company_has_user_user_id = u.user_id 
                    INNER JOIN form f2 
                        ON p.project_company_has_user_project_id = f2.form_project_id 
                WHERE 
                    f.form_question_has_answer_id IN ('19262', '21560', '23088', '22660', '14772', '18495', '18720', '21625', '19957', '20943') 
                    AND ((f2.form_template_name = "custom" 
                    AND p.project_company_has_user_garbage_collection = 0 
                    AND p.project_company_has_user_project_id = 29) 
                    AND f.form_question_has_answer_request REGEXP 'item-cadcae') 
                ORDER BY company_company_name asc
            ) as f
                ON f.company_id= c.id
于 2012-04-24T08:56:59.040 回答
1

至少有一个与搜索匹配的表单的所有客户的所有表单数据:

SELECT * FROM `form_data`
WHERE `clientid` IN (
  SELECT DISTINCT `clientid` FROM `form_data`
  WHERE `data` RLIKE '[[:<:]]motor oil[[:>:]]'
);
于 2012-04-24T09:02:56.177 回答
0

不确定 为匹配的公司保留所有表格是什么意思?不只是为了这个...

select * from clients inner join form_data on clients.id = form_data.id where form_data.[data] like '%motor  oil%'
于 2012-04-24T08:53:08.520 回答
0

这将返回为名称中也包含“机油”的公司填写的所有包含“机油”的表格:

SELECT  *
FROM    clients c
JOIN    form_data fd
ON      fd.client = c.id
WHERE   fd.data RLIKE '[[:<:]]motor oil[[:>:]]'
        AND c.name RLIKE '[[:<:]]motor oil[[:>:]]'
于 2012-04-24T08:55:23.900 回答