mysql - How to bypass a reference to an outer table in subquery? -


i've been dealing these 2 tables:

document

id    company_id    etc ======================= 1     2             x 2     2             x 

version

id    document_id   version    date_created    date_issued   date_accepted ========================================================================== 1     1             1          2013-04-29      2013-04-30    null 2     2             1          2013-05-01      null          null 3     1             2          2013-05-01      2013-05-01    2013-05-03 

there's page want list documents attributes. , add single have status each document. status can derived present date corresponding versions have. possible older version being accepted.

the query result looking this:

id    company_id    etc   status ================================== 1     2             x     accepted 2     2             x     created 

i started out making query combines dates , add status next it. works expected , when add document_id things alright.

select `status` (     select max(date_created) `date`,'created' `status` version document_id = 1     union     select max(date_issued),'issued' version document_id = 1     union     select max(date_accepted),'accepted' version document_id = 1     order date desc     limit 1 ) maxi 

when try incorporate query subquery, can't make work.

select *, (   select `status` (     select max(date_created) `date`,'created' `status`from version document_id = document.id     union     select max(date_issued),'issued' version document_id = document.id     union     select max(date_accepted),'accepted' version document_id = document.id     order date desc     limit 1   ) maxi ) `status` `document` 

this me error unknown column 'document.id' in 'where clause'. i've read around @ , figured can't reach value offer.id since it's subquery in subquery. tried take approach , statuses @ once, avoid where statement, , join them. ended next query.

select max(`date`),`status`, document_id (     select datetime_created `date`, 'created' `status`,document_id `version`     union     select datetime_issued, 'issued',document_id `version`     union     select datetime_accepted, 'accepted',document_id `version` ) dates group offer_id 

no error time realized status couldn't correct 1 since got lost during group by. i've tried combinations of 2 both flaws keep hindering me. 1 suggest how in single query without changing database? (i know saving dates in separate table things)

can normalise table designs move status / dates onto different table versions?

if no possibly this:-

select document.id, document.company_id, document.etc, case when sub1.status = 3 'accepted' when sub1.status = 2 'issued' when sub1.status = 1 'created' else null end status document inner join (     select document_id, max(case when date_accepted not null 3 when date_issued not null 2 when date_created not null 1 else null end) status      version     group document_id ) sub1 on document.id = sub1.document_id 

the subselect gets highest status document version table. each possible versions highest status returned number, , grouping on document id highest status of version. joined against document table , number version number converted text description.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -