如何使用dql从数据表中获取唯一的值? [英] How can get unique values from data table using dql?

查看:142
本文介绍了如何使用dql从数据表中获取唯一的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中有一列存储各种值的列。我想使用dql从该表中检索唯一的值。

  Doctrine_Query :: create()
- > select('rec.school')
- > from('Records rec')
- > where(rec.city ='$ city')
- > execute();

现在我只想要唯一的值。有人可以告诉我怎么做...



修改



表结构:

  CREATE TABLE IF NOT EXISTS`records`(
`id` int(11)NOT NULL AUTO_INCREMENT,
`state` varchar(255)COLLATE utf8_unicode_ci DEFAULT NULL,
`city` varchar(255)COLLATE utf8_unicode_ci DEFAULT NULL,
`school` varchar(255)COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY(`id`)
)引擎= InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci AUTO_INCREMENT = 16334;

这是我使用的查询:

  Doctrine_Query :: create()
- > select('DISTINCT rec.city')
- > from('Records rec')
- > where(rec.state ='$ state')
// - > getSql();
- > execute();

为此生成Sql可以让我:

  SELECT DISTINCT r.id AS r__id,r.city AS r__city FROM records r WHERE r.state ='AR'

现在检查sql生成的::::
DISTINCT 是在'id'列,我想要在城市列上Distinct。任何人都知道如何解决这个问题。



EDIT2



一个自动增量值。我在城市列中有一些真正的重复,如:德里和德里。现在,当我试图从中获取数据时,我正在德里两次。我如何进行如下查询:

  select DISTINCT rec.city where state =xyz; 

因为这会给我正确的输出。



任何人谁可以告诉我如何找出这个查询..?



$ b

b

解决方案

取决于你使用的是什么版本,但是我也有同样的问题 - > distinct()对我有用。

  Doctrine_Query :: create()
- > select('rec.city') - > distinct()
- > from('Records rec')
- > where(rec.state ='$ state')
- > execute();


I am having a table in which there is a column in which various values are stored.i want to retrieve unique values from that table using dql.

         Doctrine_Query::create()
                    ->select('rec.school')
                    ->from('Records rec')                   
                    ->where("rec.city='$city' ")                                    
                    ->execute();        

Now i want only unique values. Can anybody tell me how to do that...

Edit

Table Structure:

CREATE TABLE IF NOT EXISTS `records` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`state` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `city` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `school` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16334 ;

This is the Query I am using:

   Doctrine_Query::create()
          ->select('DISTINCT rec.city')
          ->from('Records rec')                   
          ->where("rec.state = '$state'")                                    
             // ->getSql();
           ->execute();                 

Generting Sql for this gives me:

SELECT DISTINCT r.id AS r__id, r.city AS r__city FROM records r WHERE r.state = 'AR'

Now check the sql generated:::: DISTINCT is on 'id' column where as i want Distinct on city column. Anybody know how to fix this.

EDIT2

Id is unique cause its an auto incremental value.Ya i have some real duplicates in city column like: Delhi and Delhi. Right.. Now when i am trying to fetch data from it, I am getting Delhi two times. How can i make query like this:

  select DISTINCT rec.city where state="xyz";

Cause this will give me the proper output.

EDIT3:

Anybody who can tell me how to figure out this query..???

解决方案

Depends on what version you are using, but I had the same issue and ->distinct() worked for me.

Doctrine_Query::create()
      ->select('rec.city')->distinct()
      ->from('Records rec')                   
      ->where("rec.state = '$state'")                                    
       ->execute();     

这篇关于如何使用dql从数据表中获取唯一的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆