mysql - SELECT unique values and the associated timestamp without having the timestamp making things unique -
i apologize poorly worded question. it's best illustrated through example , i've come far:
table "myinfo" has columns:
1. id (pk) 2. key 3. value 4. metaid
table "meta" has columns:
1. id (pk) 2. metaname 3. metavalue 4. instancenum
metaid in "myinfo" table correlates instancenum in "meta" table. value of "value" column changes on different rows metaid. think of metaid link timestamp value in "meta" table("timestamp" , value go metaname , metavalue columns respectively).
i want select distinct values of 'value' column in "myinfo". far have:
select distinct mi.key, mi.value myinfo mi join metadata meta mi.metaid=meta.instancenum , meta.key = 'timestamp' , mi.key='maxweight';
but want timestamps associated values. want output like:
key value timestamp maxweight 10 tons 15:00:05 2011-01-01 maxweight 5 tons 08:00:07 2011-10-12 maxweight 25 tons 13:05:09 2013-08-01
i can't place timestamp 1 of columns in select because return duplicate mi.attrvalue values since timestamp makes every row unique. tried putting distinct keyword behind mi.attrvalue got mysql error.
this untested grouping concat might work.
select mi.key, mi.value, meta.value myinfo mi join metadata meta on mi.metaid = meta.id mi.key = 'maxweight' , meta.key = 'timestamp' group concat(mi.key, mi.value)
although you'll still have problem of timestamp shown. example, if have 3 records given key/value pair, record shown in result set?
key value timestamp maxweight 10 tons 15:00:05 2011-01-01 maxweight 10 tons 08:00:07 2011-10-12 maxweight 10 tons 13:05:09 2013-08-01
the group query show 1 of these results - which? you'll need think ordering group (which whole new ballgame)
Comments
Post a Comment