[Zope] ZSQL, Python Script, number of records
Dieter Maurer
dieter@handshake.de
Mon, 6 Aug 2001 22:45:14 +0200 (CEST)
Milos Prudek writes:
> To check whether a record exists I'm using this:
>
> exist (ZSQL method):
> SELECT count(*) as I FROM ... WHERE ... ;
>
> Python Script:
> for x in exist(m_id=m_id):
> if x.i==0:
> print "no data"
> else:
> print "there are some data"
x=exist(m_id=m_id)[0]
....
> I tried to adapt the "exist" ZSQL to show a column from the table:
> exist2 (ZSQL method):
> SELECT count(*) as I, fname FROM ... WHERE ... ;
You may want to read the SQL restrictions applying for
accumulation functions like "count".
> It fails and it recommends to put "fname" into GROUP BY. So:
> exist3 (ZSQL method):
> SELECT count(*) as I, fname FROM ... WHERE ... GROUP BY fname;
>
> That works but IMHO it's a very ugly hack and it may breed trouble later on.
>
> Is there a more elegant way to get both values and number of values using a
> single ZSQL method?
I am not sure what you want to get.
Your "exist3" is definitely completely different from your "exist"!
While "exist" always returns a single row, "exist3" may return
any number of rows, especially 0 and many.
Each row contains the value for "FNAME" and how often it occurs.
If you want this result, the SQL query is completely natural.
Otherwise, what do you want?
Dieter