gpt4 book ai didi

sql - 如何选择所有参数匹配的记录?

转载 作者:行者123 更新时间:2023-12-03 19:36:01 26 4
gpt4 key购买 nike

我有表catalog_view。 I 包含具有指定类别的联系人。联系人可以有多个类别。我要选择联系人,属于用户类别选择的所有联系人

e.g contact "Jone" belongs to categories [1,2,3,4,5,6] and contact "Jay" belongs to categories [1,3]. If user select categories [2,3], only "Jone" should be returned, if [1]|[3]|[1,3] selected, both should be returned, if [7]|[1,7] selected, none should be returned. 

我应该如何构建查询和/或更改表来实现它?

查询至极按单个类别选择联系人
SELECT * FROM catalog_view WHERE (category_id = ?)

如果分配的类别与其中任何一个匹配,则此查询选择记录,并返回联系人行数(最多为参数数量),它们也不会强制选择所有这些。
SELECT * FROM catalog_view WHERE (category_id = ? OR category_id = ? OR category_id =?)

此查询始终返回空表,因为 catalog_view 的当前模板中的行只有一个 category_id
SELECT * FROM catalog_view WHERE (category_id = ? AND category_id = ? AND category_id =?)

联系人
CREATE TABLE IF NOT EXISTS contacts_table(_id integer primary key, title text default '',title_lower text default '',title_short text default '',photo blob, number text default '', note text default '', note_lower text default '', email text, partner int default '0', contact_id int default '0', current_user text default '', categories text default '', saved integer default '0' );

类别
CREATE TABLE IF NOT EXISTS categories_table(_id integer primary key, category_id integer, parent_id integer, name text, section_name text, current_user text, sub_name text default '' );

有类别的联系人
CREATE TABLE IF NOT EXISTS contacts_categories_table(_id integer primary key, contact_id integer, category_id integer, current_user text );

目录 View 联系人
CREATE VIEW IF NOT EXISTS catalog_view AS  SELECT contacts_table._id AS _id, contacts_table.contact_id, contacts_table.title_lower, contacts_table.note_lower, contacts_table.current_user, contacts_table.saved, contacts_table.partner, contacts_table.title, contacts_table.email, contacts_table.title_short, contacts_table.number, contacts_table.note, contacts_table.categories, categories_table.section_name, contacts_table.contact_id, categories_table.parent_id, categories_table.category_id  FROM contacts_categories_table INNER JOIN contacts_table ON(contacts_categories_table.contact_id = contacts_table.contact_id AND contacts_categories_table.current_user = contacts_table.current_user)  LEFT OUTER JOIN categories_table ON(contacts_categories_table.category_id = categories_table.category_id AND contacts_categories_table.current_user = categories_table.current_user) 

最佳答案

您可以尝试以下方法:

SELECT distinct name from catalog_view s
where NUMBER_OF_CONTACT_SELECTION =
(SELECT count(*) from catalog_view t
where t.name = s.name and
category_id in(YOUR CONTACT SELECTION HERE IN FORMAT OF 1,3,4...))

所以对于 1,3,您的查询应该是:
SELECT distinct name from catalog_view s
where 2 =
(SELECT count(*) from catalog_view t
where t.name = s.name and
category_id in(1,3))

对于 1,2,4,7 它应该是 -> 4 = (.....IN(1,2,4,7))

关于sql - 如何选择所有参数匹配的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35173810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com