MYSQL Select two distinct values -


select distinct v.id, v.make_id, v.model_id, i.attach_location, mk.make, md.model, v.made_year, u.username wsq_garage_vehicles v, wsq_garage_vehicles_gallery vg, wsq_garage_images i, wsq_garage_makes mk, wsq_garage_models md, wsq_users u v.id = i.vehicle_id , mk.id = v.make_id , md.id = v.model_id , v.user_id = u.user_id order v.date_updated desc limit 10 

the above returns

 id     make_id     model_id    attach_location     make    model   made_year   username 2   25  258     garage_vehicle-2-1373826921.jpg     ford    fiesta  2012    john 12  95  836     garage_vehicle-12-1374094864.jpg    nissan  200sx   1998    lucky307 12  95  836     garage_vehicle-12-1374095057.jpg    nissan  200sx   1998    lucky307 12  95  836     garage_vehicle-12-1374095721.jpg    nissan  200sx   1998    lucky307 10  90  752     garage_vehicle-10-1374080908.jpg    vauxhall    astra   2003    adm 8   90  756     http://i1279.photobucket.com/albums/y538/allankend...   vauxhall    cavalier    1993    muzz 8   90  756     garage_vehicle-8-1374058024.jpg     vauxhall    cavalier    1993    muzz 9   25  253     garage_vehicle-9-1374058087.jpg     ford    escort  1992    v33bot 1   25  258     garage_vehicle-1-1373755717.jpg     ford    fiesta  2005    beardy 4   43  366     garage_vehicle-4-1373916262.jpg     land rover  defender    1996    hobbs92 

what need return actual distinct id end so

 id     make_id     model_id    attach_location     make    model   made_year   username 2   25  258     garage_vehicle-2-1373826921.jpg     ford    fiesta  2012    john 12  95  836     garage_vehicle-12-1374095057.jpg    nissan  200sx   1998    lucky307 10  90  752     garage_vehicle-10-1374080908.jpg    vauxhall    astra   2003    adm 8   90  756     http://i1279.photobucket.com/albums/y538/allankend...   vauxhall    cavalier    1993    muzz 9   25  253     garage_vehicle-9-1374058087.jpg     ford    escort  1992    v33bot 1   25  258     garage_vehicle-1-1373755717.jpg     ford    fiesta  2005    beardy 4   43  366     garage_vehicle-4-1373916262.jpg     land rover  defender    1996    hobbs92 

use group by aggregate function attach_location this:

select    v.id,    v.make_id,    v.model_id,    max(i.attach_location),    mk.make,    md.model,    v.made_year,    u.username wsq_garage_vehicles v inner join wsq_garage_images            on v.id      = i.vehicle_id inner join wsq_garage_makes mk            on mk.id     = v.make_id inner join wsq_garage_models md           on md.id     = v.model_id inner join wsq_users u                    on v.user_id = u.user_id group v.id,           v.make_id,           v.model_id,           mk.make,           md.model,           v.made_year,           u.username order v.date_updated desc limit 10; 

note that:

  • i used explicit join syntax instead of old join syntax using. recommended use it.
  • there no join condition between tables wsq_garage_vehicles v , wsq_garage_vehicles_gallery vg in query, , never used in query, removed it.
  • another way achieve ignoring aggregate function attach_location , work fine in mysql, arbitrary value it.

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -