Group by with where 子句,其中两个表达式都必须正确?
Group by with where clause where both expressions have to be correct?
我有一个 table products
、artists
和 artist_product
。一个产品可能来自多个艺术家,所以 artist_product
table 会说
id = 1
product_id = 1
artist_id = 1
id = 2
product_id = 1
artist_id = 2
我现在想要的是获得所有由同一位艺术家制作的产品。因此,所有来自 Artist 1
和 Artist 2
但不是其他人(例如第三位艺术家)
的产品
假设我有上面 "table" 中解释的艺术家 1 和艺术家 2。我正在查看 ID 为 1 的产品。现在我想要所有其他产品都是同一位艺术家的(例如产品 2、3、4 等),但不是来自其他艺术家,也不是当前产品。
我写了这个查询
SELECT p.title, ap.product_id
FROM artist_product ap
INNER JOIN product p ON p.id = ap.product_id
GROUP BY product_id
HAVING COUNT(*) > 1;
现在我得到了不止一位艺术家的所有产品,对吗?但问题是,我现在只能从多个艺术家那里得到产品,我现在也需要检查艺术家。我不能做 artist_id = x AND artist_id = x
因为因为分组依据那是行不通的,我也不能做 artist_id = x OR artist_id = x
因为那不一样。
我需要解决这个问题,还需要知道我在 Doctrine 中如何解决这个问题?我目前没有 ArtistProduct
的 class,我需要创建一个并像这样吗?
$qb = $em->getRepository('\namespace\Product')->createQueryBuilder('p');
$artists = $this->getArtists();
$qb->innerJoin('ap.artist_product', 'a', 'WITH', 'a.id IN (:artists)')
->setParameter('artists', $this->getArtists())
->setMaxResults(12);
$qb->andWhere('p.id != ' . $this->getId());
return $qb->getQuery()->getResult();
这看起来不对,一定是我遗漏了什么。通常我会先在 SQL 中创建查询,然后 然后 在 Doctrine 中创建查询,但我什至没有得到正确的查询。
SELECT p.id, p.title, ap2.product_id, count(*) c
-- first select what we want - product
FROM product p
-- it must have both artists
INNER JOIN artist_product ap
ON p.id = ap.product_id AND ap.artist_id IN (?, ?)
-- catch any other artist
LEFT JOIN artist_product ap2
ON p.id = ap2.product_id AND ap2.artist_id NOT IN (?, ?)
GROUP BY p.id
-- and exclude the product, if it has one
HAVING ap2.product_id IS NULL
-- there must also be exactly 2 artists
AND c = 2;
我有一个 table products
、artists
和 artist_product
。一个产品可能来自多个艺术家,所以 artist_product
table 会说
id = 1
product_id = 1
artist_id = 1
id = 2
product_id = 1
artist_id = 2
我现在想要的是获得所有由同一位艺术家制作的产品。因此,所有来自 Artist 1
和 Artist 2
但不是其他人(例如第三位艺术家)
假设我有上面 "table" 中解释的艺术家 1 和艺术家 2。我正在查看 ID 为 1 的产品。现在我想要所有其他产品都是同一位艺术家的(例如产品 2、3、4 等),但不是来自其他艺术家,也不是当前产品。
我写了这个查询
SELECT p.title, ap.product_id
FROM artist_product ap
INNER JOIN product p ON p.id = ap.product_id
GROUP BY product_id
HAVING COUNT(*) > 1;
现在我得到了不止一位艺术家的所有产品,对吗?但问题是,我现在只能从多个艺术家那里得到产品,我现在也需要检查艺术家。我不能做 artist_id = x AND artist_id = x
因为因为分组依据那是行不通的,我也不能做 artist_id = x OR artist_id = x
因为那不一样。
我需要解决这个问题,还需要知道我在 Doctrine 中如何解决这个问题?我目前没有 ArtistProduct
的 class,我需要创建一个并像这样吗?
$qb = $em->getRepository('\namespace\Product')->createQueryBuilder('p');
$artists = $this->getArtists();
$qb->innerJoin('ap.artist_product', 'a', 'WITH', 'a.id IN (:artists)')
->setParameter('artists', $this->getArtists())
->setMaxResults(12);
$qb->andWhere('p.id != ' . $this->getId());
return $qb->getQuery()->getResult();
这看起来不对,一定是我遗漏了什么。通常我会先在 SQL 中创建查询,然后 然后 在 Doctrine 中创建查询,但我什至没有得到正确的查询。
SELECT p.id, p.title, ap2.product_id, count(*) c
-- first select what we want - product
FROM product p
-- it must have both artists
INNER JOIN artist_product ap
ON p.id = ap.product_id AND ap.artist_id IN (?, ?)
-- catch any other artist
LEFT JOIN artist_product ap2
ON p.id = ap2.product_id AND ap2.artist_id NOT IN (?, ?)
GROUP BY p.id
-- and exclude the product, if it has one
HAVING ap2.product_id IS NULL
-- there must also be exactly 2 artists
AND c = 2;