sql - MySQL Union?? Query Help -
i have 2 tables. 1 has album information, , other actual copies of albums in inventory. these have different prices based on various factors. each row in inventory table doesn't store album information directly, id of album in "albums" table.
the query want write me artist name, album name, image, etc albums table highest , lowest prices found in inventory table.
i know basic mysql i'm having lot of trouble one. can expert please me here? lot!
albums table:
- id (pk)
- title
- artist
- year
- genre
inventory table:
- id(pk)
- albumid (corresponds id in albums table)
- price
- condition
- mono
the query i've attempted looks giving me error:
select albums.artist, albums.title, albums.imgurl, albums.id albums union select max(inventory.price), min(inventory.price) albums.id = inventory.albumid limit 30 select * `albums`
the 2 parts of (two-part) union must have same number of columns, , types need same or treatable if same.
your second part doesn't have clause.
and final line separate query should separated previous 1 semi-colon.
what want is, more or less:
select a.artist, a.title, a.imgurl, a.id, max(i.price), min(i.price) albums join inventory on a.id = i.albumid --where ...conditions on artist or album... group a.artist, a.title, a.imgurl, a.id
note how using table aliases allows the select-list fit on 1 line. won't always, of course, using aliases can provide succinctness, , encourages tagging columns origin table, helps make explicit data comes (thereby improving readability , clarity , other fine qualities).
Comments
Post a Comment