python - Nested SELECT using sqlalchemy -


I want to create a query with the nested sort, but I can not find the expected results.

I have made the following question simple, so it will be easily understandable for this post.

Here I would like to create a query:

  SELECT pear_table.size, (select apple.apple_date from Apple where apple_id = pear_table.pear_id) apple_min, (select Max (lemon.lemon_date) Where citrus lemon_id = pear_table.pear_id) from (where selecting pear_id pear lemon_max pear_color = 'green') Pear_table  

note that both subqueries Use 'pear_id' in

  APPLE = wrapper.getMapper ('apple') LEMON = wrapper: This query works well when used as string.  

Now I try to make it using sqlalchemy: getMapper ('lemon') chicken = wrapper. Japmarpr ( 'pear') pear_table = select ([Pear.aple_aidi]) pear_table.append_whereclause (Pearkpir_golr == 'green') apple_min = select ([Funkmin (apples. Apple_date) .label ( 'apple_min')] ) apple_min.append_whereclause (APPLE.apple_id == pear_table.pear_id) lemon_max = select ([func.min (LEMON.apple_date) .label ( 'lemon_max')]) lemon_max.append_whereclause (LEMON.lemon_id == pear_table.pear_id) main_query = Selection ([pear_table.c.pear_id, apple_min.c.apple_id, lemon_max.c.lemon_max])

And here is the construction SQLAlchemy with this code:

< Pre> Select pear_table.size, apple_min, from lemon_max (select pear_id from pear where pear_color = 'green'), pear_table (min.apple.apple_date) Elect from where AP Apple ple_id = pear_table.pear_id) apple_min, where citrus (max (select lemon.lemon_date) lemon_id = pear_table.pear_id) lemon_max

problem by my 2 subquery That 'pear_id' is out of reach 'apples' and 'lemons', because Skylchheimer has cast subcategory in the 'FORM' section.

I tried to solve my problem using the correlative option:

  choose apple_min = ([func.min (apple.apl_date) .label (' Apple_man ')]). Related (none). Select lemon_max = ([fun.min (neeman.apl_date) .label ('neem_max')]). Correlate (none).  

Here's what I get:

  SELECT pear_table.size, apple_min, lemon_max from FROM (select pear_id pear_id pear_color = 'green') pear_table, Applemmin (select max (lemon.lemon_date) from lemon, (Select from pear_id pear, where (select minimum (apple.apple_date) apple, (pear_id pear_id where pear_color = 'green') where apple_id = pear_table.pear_id Pear_color = 'green') WHERE lemon_id = pear_table.pear_id) lemon_max  

This duplicates 'FROM pear_id ...', and duplicate me for obvious performance reasons Do not query

  • Am I doing something wrong?
  • Is there a better way to create my query?
  • Does the sclassium allow me to build the way I want it?
  • Is there any way to put subqueries directly into the main 'SELECT' clause?

I am currently using sqlalchemy 0.4, but with that I have 0.8 and the result is same.

Instead of using it (use the key):

  pear_table = (select ([Pearkpiar_aidi]) .where (Pearkpir_color == beat '')) Apple_min = (select ([Funkmin (Apli.apiel_det) Klabl ( 'Seb_minej')]) Where (Seplet.aplid == pear_table.c.pear_id)) .AS_scalar () lemon_max = (select ([fun.min (neeman.lemman_det) .label ('lemon_max')]). Where (Neeman .lemon_id = = pear_table.c.pear_id)) .AS_scalar () main_query = select ([pear_table.c.pear_id, apple_min, lemon_max]) print (main_query)  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -