Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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_Join_Exists - Fatal编程技术网

Mysql 从另一个表中选择集合中包含的行

Mysql 从另一个表中选择集合中包含的行,mysql,sql,join,exists,Mysql,Sql,Join,Exists,我有一个表“Products”,表中有产品名称和id: id | title 1 product 1 2 product 2 每个产品都可以有一系列标签。标签在表“属性”中标识: 产品与标签的关系是另一个表“标签”: 好的,我试着选择一组至少有一个特定标签的产品,以及可能选择的其他标签。以下是我现在的工作内容: SELECT products.id FROM products WHERE EXISTS ( SELECT 1 FROM Ta

我有一个表“Products”,表中有产品名称和id:

id    |  title
1        product 1
2        product 2
每个产品都可以有一系列标签。标签在表“属性”中标识:

产品与标签的关系是另一个表“标签”:

好的,我试着选择一组至少有一个特定标签的产品,以及可能选择的其他标签。以下是我现在的工作内容:

SELECT products.id
FROM products
WHERE
EXISTS
( 
    SELECT  1
    FROM Tags
    INNER JOIN Attributes ON tags.AttId = Attributes.id
    WHERE Attributes.handle = 'tag-one'
    AND (
            Attributes.handle = 'tag-two'
            OR
            Attributes.handle = 'tag-four'
        )

    AND products.id = Tags.OwnerId
)

如果我删除AND(或)部分,查询就会工作。如上所述,它没有显示错误,但也没有显示结果;我应该如何写这篇文章,这样我就可以得到一组肯定有一个标签的产品,并且通过标签句柄有/或其他指定的标签?

我喜欢使用
groupby
having
——来解决这类问题,因为我发现这种方法非常适合表达许多不同的条件。根据您的条件:

select p.*
from products p join
     tags t
     on t.OwnerId = p.id join
     attributes a
     on t.attid = a.id
group by p.id
having sum(a.handle = 'tag-one') > 0 and
       sum(a.handle in ('tag-two', 'tag-four')) > 0;

having
子句中的每个条件统计与条件匹配的行数(对于产品)。第一个表示至少有一行带有
'tag-one'
句柄。第二个表示至少有一行有另外两个句柄。

我喜欢使用
分组方式和
拥有
来处理这类问题,因为我发现这种方法非常适合表达许多不同的条件。根据您的条件:

select p.*
from products p join
     tags t
     on t.OwnerId = p.id join
     attributes a
     on t.attid = a.id
group by p.id
having sum(a.handle = 'tag-one') > 0 and
       sum(a.handle in ('tag-two', 'tag-four')) > 0;

having
子句中的每个条件统计与条件匹配的行数(对于产品)。第一个表示至少有一行带有
'tag-one'
句柄。第二个表示至少有一行包含其他两个句柄。

我认为如果执行两个单独的查询并获取交叉点,将得到您想要的

-- Get all the owner ids that have 'tag-one'
select OwnerId
from Tags t1
where AttId in
(
  select id
  from Attributes a1
  where a1.handle = 'tag-one'
)
intersect
-- Get all the owner ids that have tag-two and tag-four
select OwnerId
from Tags t2
where AttId in
(
  select id
  from Attributes a2
  where a2.handle in ('tag-two', 'tag-four')
)
;

我认为,如果您执行两个单独的查询并获取交叉点,那么将得到您想要的

-- Get all the owner ids that have 'tag-one'
select OwnerId
from Tags t1
where AttId in
(
  select id
  from Attributes a1
  where a1.handle = 'tag-one'
)
intersect
-- Get all the owner ids that have tag-two and tag-four
select OwnerId
from Tags t2
where AttId in
(
  select id
  from Attributes a2
  where a2.handle in ('tag-two', 'tag-four')
)
;