我不确定我想要做什么是可能的,但我一直在尝试采用不同的方法来解决我需要的问题,但到目前为止,我已经空手而归。
让我们说我有2张桌子(只是一个例子,在我的情况下,还有更多的内容+更多的数据)
一个称为客户端,另一个称为form_data。 我们在clients表中有多个客户端,在form_data表中,我们为clients表中的每个公司提供了多行。在form_data中,我们存储来自不同表单的序列化数据。 (身份证和数据)
我正在从form_data表中提取所有记录,我正在尝试在数据列上使用正则表达式来过滤,例如在其中找到值'motor oil'。
我想要一种方法来做这个过滤器,但过滤公司而不是表格..所以我想找到其中有“机油”的表格,并删除没有这个的公司的所有条目匹配,但我希望保留所有匹配公司的表格。
我可以发布我的查询,但它相当长,我认为如果我们能够解决上述问题,那么我应该将其实现到实际查询中。
此致
编辑:
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种形式在查询中丢失..我想保留它们。我想要的是任何没有找到任何形式的公司(机油)将所有表格从搜索中移除。
答案 0 :(得分: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%'
您可以使用适当的过滤器从表单表中选择任何选择相关行的查询。
你也可以使用左外连接进行反转,其中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
答案 1 :(得分:1)
至少有一个与搜索匹配的表单的所有客户的所有表单数据:
SELECT * FROM `form_data`
WHERE `clientid` IN (
SELECT DISTINCT `clientid` FROM `form_data`
WHERE `data` RLIKE '[[:<:]]motor oil[[:>:]]'
);
答案 2 :(得分:0)
不确定通过为匹配的公司保留所有表单是什么意思? 不只是在这... ...
select * from clients inner join form_data on clients.id = form_data.id where form_data.[data] like '%motor oil%'
答案 3 :(得分: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[[:>:]]'