mysql - WHERE fieldname IS MIN(somedate) Is there a way to write this without using a subquery? -


our server having hard time running query. hoping me out suggesting way of re-writing same results without sub query.

the purpose of subquery use min in clause: i'd results returned earliest co.created date.

so wish this:

select          my.data account_id,         co.id user_id,         def.def_medium,         co.created (tables associated joins) min(co.created) 

but cannot came this:

select      my.data account_id,     co.id user_id,     def.def_medium,     co.created     abc_emails.cid208 co inner join abc_emails.def208 def on def.eid = co.id inner join abc_emails.my208 on my.eid = co.id , my.my_id = 2765 inner join (select                  my.data account_id,                 min(created) created                              abc_emails.my208             inner join abc_emails.cid208 co on co.id = my.eid                 my_id = 2765             group account_id             ) b1 on b1.account_id = my.data group account_id, user_id, def_medium 

but it's been running long time and, given experience server, i'm booted off error message telling me it's timed out.

is possible i'm doing no subquery?

your query want use:

select          my.data account_id,         co.id user_id,         def.def_medium,         co.created (tables associated joins) min(co.created) 

is not query. read out loud yourself. "i want data, minimum date.....". incomplete thought. have specify want accomplish in clause. should read "i want data, minimum date of co table, "

for example:

select          my.data account_id,         co.id user_id,         def.def_medium,         co.created (tables associated joins) my.date > min(co.created) 

if want oldest items, , know account_id, user_id, def_medium, can order list created date ascendingly , grab number of results want.

select  my.data account_id,         co.id user_id,         def.def_medium,         co.created (tables associated joins) order co.created asc limit numberofresults 

you're trying find oldest account_id. if have 10 rows, same id, want find 1 min(co.created). equivalent of mssql row_number partition: stackoverflow.com/questions/1895110/row-number-in-mysql


Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -