[web2py] Re: create table as select.

2011-01-29 Thread DenesL
To answer your first question I will illustrate it with an example:

#models

db.define_table('person',
  Field('name'),
  Field('age','integer'),
  format='%(name)s'
)

db.define_table('dog',
  Field('name'),
  Field('owner','reference person'),
  format='%(name)s'
)

#controller
def build_new_table():
  # drop table if already there
  if 'newtable' in db.tables:
db.newtable.drop()

  # create table
  db.define_table('newtable',
# first new field same as person.name
db.person.name,
# rename second since fields need distinct names
Field('dog_name',type=db.dog.name.type,length=db.dog.name.length)
  )

  # SELECT person.name, dog.name FROM person JOIN dog ON
(person.id=dog.owner)
  rr=db(db.person.id==db.dog.owner).select(db.person.name,db.dog.name)

  # add records to newtable
  for r in rr:
db.newtable.insert(name=r.person.name,dog_name=r.dog.name)
  db.commit()


For more info on queries see chapter 6 of the book:
http://web2py.com/book/default/chapter/06

To show the records you could use powertable plugin by Bruno, who
recently won the second web2py app exhibition.
But a simple SQLTABLE will do too.



On Jan 29, 10:07 am, vortex  wrote:
> Because I have a controller that presents data for a table. So to
> present data which is a join of several tables, I would build a table
> and then use that same controller. Where can I find more information
> on how to make more complex queries and presenting data with web2py?
>



[web2py] Re: create table as select.

2011-01-29 Thread vortex
Because I have a controller that presents data for a table. So to
present data which is a join of several tables, I would build a table
and then use that same controller. Where can I find more information
on how to make more complex queries and presenting data with web2py?


On Jan 28, 6:27 pm, DenesL  wrote:
> It can be done, but why do you want to do it?.
>
> On Jan 28, 10:42 am, vortex  wrote:
>
> > Is it possible to join to tables and create a new table based on this
> > join using only DAL?
>
> > CREATE TABLE newtable AS SELECT  ta.key, ta.col1, tb.col2  FROM ta
> > JOIN tb.col1 on (ta.key=tb.key)
>
>


[web2py] Re: create table as select.

2011-01-28 Thread DenesL

It can be done, but why do you want to do it?.

On Jan 28, 10:42 am, vortex  wrote:
> Is it possible to join to tables and create a new table based on this
> join using only DAL?
>
> CREATE TABLE newtable AS SELECT  ta.key, ta.col1, tb.col2  FROM ta
> JOIN tb.col1 on (ta.key=tb.key)