简体   繁体   中英

How to store multiple items for one ID?

I'm trying to set up a mysql database and it is a real brainf*ck for me.. This is my problem. Lets say I have a list of devices. like phone 1 , Phone 2 and so on. I give every device a ID named Device_ID. Every phone has accessories, but some of these accessories can be used by multiple phones.

Now I want to see all compatible accessories for one specific device. and store this in a database without having to type every accessory multiple times for every compatible device.

any help how to do this ?

thnx in advance

You create three tables, phones , accessories and phone_accessories . The third one is a joining table that only contains a phone_id and an accessory_id . Each accessory is given one row in the accessories table, and when you say that an accessory can be used for a phone, you create one row in the phone_accessories table that contains the ID of the phone and accessory. Then you can use MySQL joins to get all accessories for a phone or all phones for an accessory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM