Mysql 在SQL中搜索数据

Mysql 在SQL中搜索数据,mysql,sql,Mysql,Sql,请看下表: 我正在构建一个搜索引擎,根据对类别id和值id的搜索,返回卡id值 为了更好地解释搜索机制,假设我们试图通过提供汽车在每个类别(category\u id)中应该包含哪些部分(value\u id)的信息来查找汽车(category\u id) 例如,我们可能想找到一辆汽车(card\u id),其中类别“燃料类型”(category\u id)的值为“柴油”(value\u id),类别“变速箱”(category\u id)的值为“手动”(value\u id) 我的问题是,我

请看下表:

我正在构建一个搜索引擎,根据对
类别id
值id
的搜索,返回
卡id

为了更好地解释搜索机制,假设我们试图通过提供汽车在每个类别(
category\u id
)中应该包含哪些部分(
value\u id
)的信息来查找汽车(
category\u id

例如,我们可能想找到一辆汽车(
card\u id
),其中类别“燃料类型”(
category\u id
)的值为“柴油”(
value\u id
),类别“变速箱”(
category\u id
)的值为“手动”(
value\u id

我的问题是,我的知识不足以构建一个查询,该查询将返回
card\u id
s,其中包含多对
category\u id
value\u id

例如,如果我想搜索一辆装有柴油发动机的汽车,我可以构建如下查询:

从类别id=1、值id=2的汽车中选择卡片id

其中,
category_id=1
为“燃料类型”类别,
value_id=2
为“柴油”

我的问题是,如何构建一个查询来查找更多的类别-值对?例如,我想寻找配备手动变速箱的柴油车


任何帮助都将不胜感激。提前感谢。

您可以使用聚合和
have
子句来完成此操作:

SELECT card_id
FROM cars
GROUP BY card_id
HAVING SUM(category_id = 1 AND value_id = 2) > 0 AND
       SUM(category_id = 3 and value_id = 43) > 0;
having
子句中的每个条件统计与给定条件匹配的行数。您可以添加任意数量的条件。例如,第一个表示至少有一行的类别为1,值为2


您可以使用聚合和具有子句的
来执行此操作:

SELECT card_id
FROM cars
GROUP BY card_id
HAVING SUM(category_id = 1 AND value_id = 2) > 0 AND
       SUM(category_id = 3 and value_id = 43) > 0;
having
子句中的每个条件统计与给定条件匹配的行数。您可以添加任意数量的条件。例如,第一个表示至少有一行的类别为1,值为2


您可以使用聚合和具有
子句的
来执行此操作:

SELECT card_id
FROM cars
GROUP BY card_id
HAVING SUM(category_id = 1 AND value_id = 2) > 0 AND
       SUM(category_id = 3 and value_id = 43) > 0;
having
子句中的每个条件统计与给定条件匹配的行数。您可以添加任意数量的条件。例如,第一个表示至少有一行的类别为1,值为2


您可以使用聚合和具有
子句的
来执行此操作:

SELECT card_id
FROM cars
GROUP BY card_id
HAVING SUM(category_id = 1 AND value_id = 2) > 0 AND
       SUM(category_id = 3 and value_id = 43) > 0;
having
子句中的每个条件统计与给定条件匹配的行数。您可以添加任意数量的条件。例如,第一个表示至少有一行的类别为1,值为2


另一种方法是创建一个用户定义的函数,该函数接受一个属性/值对表,并返回一个匹配汽车表。这样做的优点是允许任意数量的属性/值对,而无需使用动态SQL

--Declare a "sample" table for proof of concept, replace this with your real data table
DECLARE @T TABLE(PID int, Attr Int, Val int)
--Populate the data table
INSERT INTO @T(PID , Attr , Val) VALUES (1,1,1), (1,3,5),(1,7,9),(2,1,2),(2,3,5),(2,7,9),(3,1,1),(3,3,5), (3,7,9)

--Declare this as a User Defined Table Type, the function would take this as an input
DECLARE @C TABLE(Attr Int, Val int)
--This would be populated by the code that calls the function
INSERT INTO @C (Attr , Val) VALUES (1,1),(7,9)

--The function (or stored procedure) body begins here
--Get a list of IDs for which there is not a requested attribute that doesn't have a matching value for that ID
SELECT DISTINCT PID 
FROM @T as T 
WHERE NOT EXISTS (SELECT C.ATTR FROM @C as C 
                  WHERE NOT EXISTS (SELECT * FROM @T as I 
                                    WHERE I.Attr = C.Attr and I.Val = C.Val and I.PID = T.PID ))

另一种方法是创建一个用户定义的函数,该函数接受一个属性/值对表,并返回一个匹配汽车表。这样做的优点是允许任意数量的属性/值对,而无需使用动态SQL

--Declare a "sample" table for proof of concept, replace this with your real data table
DECLARE @T TABLE(PID int, Attr Int, Val int)
--Populate the data table
INSERT INTO @T(PID , Attr , Val) VALUES (1,1,1), (1,3,5),(1,7,9),(2,1,2),(2,3,5),(2,7,9),(3,1,1),(3,3,5), (3,7,9)

--Declare this as a User Defined Table Type, the function would take this as an input
DECLARE @C TABLE(Attr Int, Val int)
--This would be populated by the code that calls the function
INSERT INTO @C (Attr , Val) VALUES (1,1),(7,9)

--The function (or stored procedure) body begins here
--Get a list of IDs for which there is not a requested attribute that doesn't have a matching value for that ID
SELECT DISTINCT PID 
FROM @T as T 
WHERE NOT EXISTS (SELECT C.ATTR FROM @C as C 
                  WHERE NOT EXISTS (SELECT * FROM @T as I 
                                    WHERE I.Attr = C.Attr and I.Val = C.Val and I.PID = T.PID ))

另一种方法是创建一个用户定义的函数,该函数接受一个属性/值对表,并返回一个匹配汽车表。这样做的优点是允许任意数量的属性/值对,而无需使用动态SQL

--Declare a "sample" table for proof of concept, replace this with your real data table
DECLARE @T TABLE(PID int, Attr Int, Val int)
--Populate the data table
INSERT INTO @T(PID , Attr , Val) VALUES (1,1,1), (1,3,5),(1,7,9),(2,1,2),(2,3,5),(2,7,9),(3,1,1),(3,3,5), (3,7,9)

--Declare this as a User Defined Table Type, the function would take this as an input
DECLARE @C TABLE(Attr Int, Val int)
--This would be populated by the code that calls the function
INSERT INTO @C (Attr , Val) VALUES (1,1),(7,9)

--The function (or stored procedure) body begins here
--Get a list of IDs for which there is not a requested attribute that doesn't have a matching value for that ID
SELECT DISTINCT PID 
FROM @T as T 
WHERE NOT EXISTS (SELECT C.ATTR FROM @C as C 
                  WHERE NOT EXISTS (SELECT * FROM @T as I 
                                    WHERE I.Attr = C.Attr and I.Val = C.Val and I.PID = T.PID ))

另一种方法是创建一个用户定义的函数,该函数接受一个属性/值对表,并返回一个匹配汽车表。这样做的优点是允许任意数量的属性/值对,而无需使用动态SQL

--Declare a "sample" table for proof of concept, replace this with your real data table
DECLARE @T TABLE(PID int, Attr Int, Val int)
--Populate the data table
INSERT INTO @T(PID , Attr , Val) VALUES (1,1,1), (1,3,5),(1,7,9),(2,1,2),(2,3,5),(2,7,9),(3,1,1),(3,3,5), (3,7,9)

--Declare this as a User Defined Table Type, the function would take this as an input
DECLARE @C TABLE(Attr Int, Val int)
--This would be populated by the code that calls the function
INSERT INTO @C (Attr , Val) VALUES (1,1),(7,9)

--The function (or stored procedure) body begins here
--Get a list of IDs for which there is not a requested attribute that doesn't have a matching value for that ID
SELECT DISTINCT PID 
FROM @T as T 
WHERE NOT EXISTS (SELECT C.ATTR FROM @C as C 
                  WHERE NOT EXISTS (SELECT * FROM @T as I 
                                    WHERE I.Attr = C.Attr and I.Val = C.Val and I.PID = T.PID ))

您的解决方案按预期工作。感谢您的代码和解释,以及您的时间。您的解决方案按预期工作。感谢您的代码和解释,以及您的时间。您的解决方案按预期工作。感谢您的代码和解释,以及您的时间。您的解决方案按预期工作。感谢您的代码和解释,以及您的时间。