douxunzui1519 2016-04-21 15:32
浏览 574

在MySQL中连接表与JSON数据

I am wondering which is the best approach to handle relationships in my data.

My person table currently stores 'name' and 'url' for multiple instances of the following (column names changed for simplicity):

  1. Favourite books
  2. Favourite websites
  3. Favourite shops
  4. Favourite places

So, for each of the above, I have a column in the person table containing a JSON string. For example, for a single person, I might have the following in the 'favourite_shops' column:

[{"name":"john lewis", "url":"http://www.johnlewis.com"},{"name":"tesco", "url":"http://www.tesco.co.uk"}]

And the following in the 'favourite_websites' column

[{"name":"bbc", "url":"http://www.bbc.co.uk"},{"name":"the guardian", "url":"http://www.guardian.co.uk"}]

Obviously these JSON strings can contain many more items, and this is repeated for the other two columns - 'favourite_books' and 'favourite_places'.

So, one query to the database can return all the data I need. But I must then loop through the JSON in AngularJS for display on the front end.

My question is this - would it be beneficial (especially due to the front end processing required), to have 4 join tables instead of the columns listed above?

Or perhaps a single jointable (as the 4 share the same properties, name and url) with an additional column indicating which of the 4 the row refers to?

Things to add:

  1. I am using Laravel 5.2 and Eloquent
  2. It is very unlikely I will ever need to display all 'persons' AND their related data at once, the view will be one person at a time.
  3. It is also very unlikely I would ever need to query specific items - ie 'favourite_shops' named 'john_lewis' - the data is supplementary to a person.
  4. The relationship between person and the 4 options is one to many - persons will not need to share the data, any matching entries are simply additional rows. The actual columns I am using are highly unlikely to have need to share data between persons.
  5. I am building the above as a RESTful api, so all data will be json_encoded.
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#python#的问题:功能监听网页
    • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
    • ¥15 怎么让wx群机器人发送音乐
    • ¥15 fesafe材料库问题
    • ¥35 beats蓝牙耳机怎么查看日志
    • ¥15 Fluent齿轮搅油
    • ¥15 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群