Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql从两(或更多)行匹配的表中选择,_Mysql_Sql - Fatal编程技术网

mysql从两(或更多)行匹配的表中选择,

mysql从两(或更多)行匹配的表中选择,,mysql,sql,Mysql,Sql,假设我们的数据库中有衬衫。所有的衬衫都有尺寸和颜色(还有许多其他的选择,但现在我想用两件来确定) 该表类似于: +-------------------------+--------------+--------------+-----------+ | variation_attributes_id | variation_id | attribute_id | option_id | +-------------------------+--------------+------------

假设我们的数据库中有衬衫。所有的衬衫都有尺寸和颜色(还有许多其他的选择,但现在我想用两件来确定)

该表类似于:

+-------------------------+--------------+--------------+-----------+
| variation_attributes_id | variation_id | attribute_id | option_id |
+-------------------------+--------------+--------------+-----------+
|                       1 |            1 | size         | s         |
|                       2 |            1 | color        | red       |
|                       3 |            2 | size         | m         |
|                       4 |            2 | color        | red       |
|                       5 |            3 | size         | s         |
|                       6 |            3 | color        | green     |
|                       7 |            4 | size         | m         |
|                       8 |            4 | color        | green     |
+-------------------------+--------------+--------------+-----------+

如何选择唯一的
变体\u id
。例如,
size
S
color
Green
时,应返回
variation\u id
==3。

您可以在
where
子句中提供必要的条件,并在
子句中对它们的出现次数进行计数

select variation_id
from tbl
where ( attribute_id = 'size' and option_id = 's' )
   or ( attribute_id = 'color' and option_id = 'green' )
group by variation_id
having count(*) = 2

您可以使用
存在
和子查询:

SELECT variation_id
FROM your_table t1
WHERE attribute_id = 'color'
  AND option_id  = 'green'
  AND EXISTS (SELECT 1
              FROM your_table t2
              WHERE t1.variation_id = t2.variation_id
                AND t2.attribute_id = 'size'
                AND t2.option_id = 's');

您可以通过一个简单的
JOIN
(self-JOIN,意思是在特定条件下将表本身连接起来)。假设您的表名为
shirts
,则如下所示:

SELECT DISTINCT s1.variation_id 
FROM shirts s1
JOIN shirts s2 ON s1.variation_id = s2.variation_id
WHERE (s1.attribute_id = 'size' AND s1.option_id='s')
  AND (s2.attribute_id = 'color' AND s2.option_id='green')
它将返回所有具有所需颜色和大小的变体id,过滤掉所有其他变体id

如果您有像
(变体id、属性id、选项id)这样的索引,那么该查询将工作正常,并且其性能也将正常

更重要的是,此查询允许您获得具有更复杂参数的
变体ID
,使用
HAVING COUNT(*)=?
要困难得多

例如,您需要查找所有大小为“s”或“m”、颜色为“绿色”或“黄色”的
变体ID

SELECT DISTINCT s1.variation_id 
FROM shirts s1
JOIN shirts s2 ON s1.variation_id = s2.variation_id
WHERE (s1.attribute_id = 'size' AND s1.option_id IN ('s', 'm'))
  AND (s2.attribute_id = 'color' AND s2.option_id IN ('green', 'yellow'))

希望这有帮助。

这个解决方案正是我想要的。选择多个选项也是问题的一部分,因此在这一点上,这对我来说是最好的答案。
SELECT T1.`variation_id`
FROM(
(SELECT *
 FROM table_name
WHERE `attribute_id` = 'size') T1 
INNER JOIN
  (SELECT *
     FROM table_name
    WHERE `option_id ` = 's') T2 ON T1.`variation_id` = T2.`variation_id`)