Mysql 如何检查列中的值是否都属于数组?
现提供以下表格:Mysql 如何检查列中的值是否都属于数组?,mysql,sql,mariadb,Mysql,Sql,Mariadb,现提供以下表格: +----------------------+ | Tables_in_automobile | +----------------------+ | car has options | | car sold | | company | | customer | | members | | model | | model has opti
+----------------------+
| Tables_in_automobile |
+----------------------+
| car has options |
| car sold |
| company |
| customer |
| members |
| model |
| model has options |
| options |
+----------------------+
MariaDB [automobile]> describe model;
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| company name | varchar(100) | NO | PRI | NULL | |
| model name | varchar(100) | NO | PRI | NULL | |
| number of seats | int(10) | NO | | NULL | |
| cost | int(10) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
MariaDB [automobile]> describe options;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| option name | varchar(100) | NO | | NULL | |
| cost | int(10) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
MariaDB [automobile]> describe `model has options`;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| company name | varchar(100) | NO | MUL | NULL | |
| model name | varchar(100) | NO | | NULL | |
| option id | int(10) | YES | MUL | NULL | |
+--------------+--------------+------+-----+---------+-------+
我需要找到满足以下条件的特定车型:
座位数>=某个值
这辆车有一套选择
下面的查询满足第一个条件,但如果任何汽车具有通过查询提供的选项集中的任何一个或多个,则它将包含在结果中。我只需要那些提供了确切选项的汽车:
SELECT `company name`, `model name`, `number of seats`,
SUM(`options`.`cost`)+`model`.`cost` AS 'cost'
FROM `model` NATURAL JOIN `model has options` JOIN `options`
WHERE `model has options`.`option id` = `options`.`id`
AND `number of seats`>=3
AND `option id` IN (3,4,7)
GROUP BY `company name`, `model name`
ORDER BY `cost`
HAVING子句可用于确保不同匹配选项的数量等于提供的选项数量
SELECT `company name`, `model name`, `number of seats`,
SUM(`options`.`cost`)+`model`.`cost` AS 'cost'
FROM `model` NATURAL JOIN `model has options` JOIN `options`
WHERE `model has options`.`option id` = `options`.`id`
AND `number of seats`>=3
AND `option id` IN (3,4,7)
GROUP BY `company name`, `model name`
HAVING COUNT(DISTINCT (`option id`)) = 3
ORDER BY `cost`
另一种方法
SELECT Field FROM model
WHERE FIND_IN_SET(Field, "company name,model name,number of seats") = 0;
类似的内容将查找不在该数组中的任何字段值。返回空集表示已找到所有。所以另一种方法是从
限制:由于逗号用作分隔符,因此数组中的任何项都不能有逗号。具有count[distinct]…=谢谢,这很有帮助。但我需要更精确。有没有办法检查这些选项是否与数组中指定的选项完全一致?in和COUNTDISTINCT的组合将确保这一点-在这个查询中没有三个不同的选项不是3、4和7。是的,我意识到了这一点。谢谢!我在php中解决了这个问题。我会试试这个。这应该是正确的。谢谢