我使用Symfony 3.1 + Doctrine GEDMO扩展(通过StofDoctrineExtensionsBundle).我已将我的实体设置为具有可排序行为:
<?php
namespace AppBundle\Entity\Manual;
use AppBundle\Entity\Identifier;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Table(name="manual_pages")
* @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
*/
class Manual
{
use Identifier;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank(message="Toto pole musí být vypln?no")
*/
private $title;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank(message="Toto pole musí být vypln?no")
*/
private $content;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Manual\ManualImage", mappedBy="manual")
* @ORM\OrderBy({"position"="ASC"})
*/
private $images;
/**
* @Gedmo\SortablePosition
* @ORM\Column(type="integer", nullable=false)
*/
private $position;
/**
* @return mixed
*/ …Run Code Online (Sandbox Code Playgroud) 在我的例子中显示更好:
我有表,存储用户的答案来自一个大表格.每个表格有139个问题.这些问题存储在不同的表中,需要时使用questionID连接.对于每个用户,都有一个ID.我现在需要制作过滤器,以便仅显示与特定问题的一个或多个答案匹配的用户.
例如,我想要用户,问题14回答"是",问题54不为空,问题100大于10.这是表格的外观:
**userID** | **questionID** | **answer**
1 14 "yes"
1 54 "something"
1 100 "9"
2 14 "no"
2 54 "north
2 100 "50"
3 14 "yes"
3 54 "test"
3 100 "12"
Run Code Online (Sandbox Code Playgroud)
结果我只想返回userID 3,因为它符合所有条件.
使用ColdFusion很容易实现,因为它允许查询查询结果,但在PHP中我没有找到任何方法.重要的是有机会添加随机数量的问题,而不仅仅是这个例子中的三个.
我有两张桌子,products和meta。它们是 1:N 关系,其中每个产品行通过外键至少有一个元行。
(即 SQLfiddle:http ://sqlfiddle.com/#!15/c8f34/1 )
我需要加入这两个表,但我只需要过滤独特的产品。当我尝试此查询时,一切正常(返回 4 行):
SELECT DISTINCT(product_id)
FROM meta JOIN products ON products.id = meta.product_id
Run Code Online (Sandbox Code Playgroud)
但是当我尝试选择所有列时,DISTINCT 规则不再适用于结果,因为返回的是 8 行而不是 4 行。
SELECT DISTINCT(product_id), *
FROM meta JOIN products ON products.id = meta.product_id
Run Code Online (Sandbox Code Playgroud)
我尝试了很多方法,比如尝试DISTINCT或GROUP BY子查询,但总是得到相同的结果。