Development

HowtoSelectDistinct

You must first sign up to be able to contribute.

Version 5 (modified by anonymous, 12 years ago)
--

i needed for one of my projects a function to SELECT DISTINCT types form the database, so i added following function

  public function getDistinctTypes()
  {
    $con = Propel::getConnection(self::DATABASE_NAME);
    $sql = "SELECT DISTINCT type FROM ".self::TABLE_NAME." ORDER BY type ASC";
    $rs = $con->executeQuery($sql, ResultSet::FETCHMODE_NUM);

    $result = array();
    while ($rs->next())
      {
        $result[] = $rs->getString(1);
      }

    return $result;
  }

afaik the correct way with propel is

<?php
  public function getDistinctTypes()
  {
    $crit=new Criteria();
    $crit->addAscendingOrderByColumn(TablenamePeer::TYPE);
    $crit->setDistinct();
    return TablenamePeer::doSelect($crit);
  }

autant pour moi