QQ::Distinct()

thread: 9 messages  |  last: about 4 years ago  |  started: wednesday, march 14, 2007, 9:16 am pdt


#1  |  platini (Boston, MA) United States of America
Wednesday, March 14, 2007, 9:16 AM PDT

(as a motivation speech)I'm trying to utilize as much QQuery as possible on any apps,
so I might be missing something to use the QQ::Distinct(),

$this->lstComputer = new QListBox($ths);
$this->lstComputer->AddItem('','');

$objComputerArray = ComputerName::QueryArray(
QQ::IsNotNull(QQN::ComputerName()->ComputerNames),
QQ::Clause(
           QQ::Distinct(QQN::ComputerName()->ComputerNames)));

right now QQ::Distinct() doesn't seem to work,

the result should populate a list of all computernames removing any duplicates...as in SELECT DISTINCT(computernames) FROM computername

#2  |  VexedPanda (Calgary, AB) Canada
Wednesday, March 14, 2007, 1:12 PM PDT

QQ::Distinct doesn't take any parameters, and simply causes a Select Distinct *, as you can verify by turning on database profiling.

If you want to specify a certain column to limit it to, you're going to have to hack the QQuery.class.php file. :(

#3  |  Matthew Lieder (Apple Valley, MN) United States of America
Wednesday, March 14, 2007, 6:27 PM PDT

I'm going to need that functionality soon; any idea of how complex a hack it would be?

#4  |  Mike Ho (San Diego, CA) United States of America Qcodo Administrator
Thursday, March 15, 2007, 8:16 AM PDT

Actually... QQ::Distinct() is kind of silly.  QQ is based on restoring full objects and not on doing partial restores of rows.  DISTINCT is basically dependent upon partial restores.

So by definition having distinct in Qcodo Query is pointless. =)

My bad.

Anyway, the suggestion moving forward, if you need to do distinct-type of queries, is to just use Ad Hoc manual SQL to do it.

Chances are, when selecting distinct, you're not wanting to restore full objects anyway (or at least, it doesn't make sense to), so utilizing things like InstantiateDbRow/Result just doesn't make any sense.

#5  |  VexedPanda (Calgary, AB) Canada
Thursday, March 15, 2007, 8:36 AM PDT

I will note that the current functionality of Distinct is still needed when doing complex conditions, such as over many-to-many tables in order to prevent duplicates from being returned. So it's not completely pointless, though without my hack to make it possible to count distinct, even that is hard to use for some things like datagrids.

#6  |  Mike Ho (San Diego, CA) United States of America Qcodo Administrator
Thursday, March 15, 2007, 8:45 AM PDT

Actually yes, that's a good point.  And that is the main reason why distinct is there in the first place (don't know why I forgot to mention that in my previous post).

But for very straightforward distinct-functionality things like SELECT DISTINCT(first_name) FROM person, you'll have to do that manually in an ad hoc query, especially b/c it logically doesn't fit within the context of qcodo queries.

#7  |  GiBSwiss Switzerland
Thursday, August 16, 2007, 8:05 AM PDT

So, what is QQ::Distinct() for ?!?

If I understood well, on the code:

$objParticipantsArray = Participations::QueryArray(
            QQ::AndCondition (
                QQ::Equal(QQN::Participations()->ConcoursId, $idConcours),
                QQ::Equal(QQN::Participations()->Reponse, 3)
            ),
            QQ::Clause(
                QQ::Distinct(),
                QQ::OrderBy(QQN::Participations()->Nom, QQN::Participations()->Prenom)
      )
    );

As Icannot give any parameter to QQ::Distinct() I cannot get rows distingued by a specified column... So I MUST use standard SQL query. A I right?

#8  |  VexedPanda (Calgary, AB) Canada
Thursday, August 16, 2007, 1:31 PM PDT

Since you aren't joining any many-to-many tables in that query, I don't expect you to ever get duplicates even with the Distinct clause left out...

And there has never been a way in SQL to do a distinct on only certain columns while returning others. That's what the Group By clause is for.

So I don't really understand your question. What manual SQL are you trying to replicate?

BTW: It turns out that Select Distinct is almost never a good idea for performance reasons. Instead your SQL should be designed to prevent duplicates in the result entirely.

#9  |  GiBSwiss Switzerland
Friday, August 17, 2007, 4:00 AM PDT

Your absolutely right, VexedPanda. DISTINCT was not the right way do do. I used GROUP BY with success! Sorry for the “noise”...



Copyright © 2005 - 2012, Quasidea Development, LLC
This open-source framework for PHP is released under the terms of The MIT License.