php - How to make a MYSQL query to include rows once of a column based on another? -


seems simple question knows sql, not me.

anyhow, here sample table:
sample table

primary (and only) key on id. scenario fallows. user may add images. newly added images inserted comune_number value 0 avoiding duplicates (on file name = image via php). images inserted can assigned category table. same image can assigned many categories, each inserted new row category id (comune_number). relation between 2 tables on comune_number.

i show images, checkbox checked assigned already.
question simple. how include all images once, same image assigned, include comune_number instead of 0. don't care order. result achieve this:
sample result

i'm aware of group by, if try

mysql_query("select * banner `comune_number` = 0");   

or

mysql_query("select * banner group `image`");   

i end same (not wanted) result.

most have combine 2 queries in one, can't figure out , how.

note1: have tried many combinations in phpmyadmin, based on (little) knowledge , on found google (including stackoverflow), none of them resulted shown.

note2: mysql version 5.1

either mysql or combined php solutions welcome.

edit

i need keep comune_number. when show images, need know id.
queries must rely on comune_number, when need comune_number = 10, result should on second image above.

edit2

it seems wasn't made myself clear. want, when user watching category of id 10, show him images once, , mark ones assigned category.
example here. if user watching category (comune_number) of id=9, show every picture once, , mark 2 assigned it.

enter image description here

based on sqlfiddle , comments here updated query:

   select r.*       (select b.*               banner b             b.comune_number = 9 union            select b2.*              banner b2             b2.comune_number = 0          group b2.image) r  group r.image; 

live demo.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -