سؤال

My MySql Table looks like this:

| id | picID      | name_Gina | name_Tom | name_Liz |
|----|------------|-----------|----------|----------|
| 1  | DSC057.jpg | 1         | 0        | 0        |
| 2  | DSC622.jpg | 1         | 0        | 1        |
| 3  | DSC624.jpg | 0         | 1        | 0        |
| .. | ..         | ..        | ..       | ..       |

and so on. The name_ Prefixes are my Persons that i associate with a certain pic. Now I would like to Display a Photo eg. DSC622.jpg and echo that Gina and Liz are marked on this Photo. So i need the Column Names (name_Gina & name_Liz) where picID = 1?

Sg like this:

SHOW COLUMNS FROM $table LIKE 'name_%' WHERE id = $id

Can anyone give me a hint? Maybe I should use two Tables, one with Names and a second with the picID and join them together?

هل كانت مفيدة؟

المحلول

You should really change your DB design to something like this

pictures table
--------------
id
name


users table
-----------
id
name
birth_date


user_pictures table
-------------------
picture_id
user_id

The user_pictures table would contain a record for every user-picture relation.

Example data

pictures
id   name
1    dsc_01.jpg
2    dsc_04.jpg

users
id  name
1   tom
2   jane

user_pictures
picture_id user_id
1          1
1          2
2          2

You can then select all pictures a specfic user is accociated with like this

select p.name
from pictures p
join user_pictures up on up.picture_id = p.id
join users u on up.user_id = u.id
where u.name = 'tom'
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top