简体   繁体   English

MySQL中有很多关系的方式是什么?

[英]What is the way for a has many relationship in MySQL?

I got a database table 'books', and a table 'reviews'.我有一个数据库表“books”和一个表“评论”。 One book can contain many reviews (one-to-many).一本书可以包含许多评论(一对多)。 I want the data returned in a nested multidimensional array, but I get all the different rows returned with a JOIN query.我想要在嵌套的多维数组中返回数据,但是我通过 JOIN 查询返回了所有不同的行。 Isn't this very performance and bandwidth inefficient?这不是性能和带宽效率很低吗? The server (NodeJS) needs to download alot of duplicate data.服务器(NodeJS)需要下载大量重复数据。 Is there another way to structure a hasmany relationship in MySQL, without downloading duplicate data?是否有另一种方法可以在不下载重复数据的情况下在 MySQL 中构建多维关系? Or need I to switch to PostgreSQL for example?或者我需要切换到 PostgreSQL 吗?

If you don't want the duplication of values from the books table, then run two queries:如果您不希望books表中的值重复,则运行两个查询:

SELECT ... FROM books WHERE id = ?;

Then a separate query:然后是一个单独的查询:

SELECT ... FROM reviews WHERE book_id = ?;

Not every operation must be done in a single SQL query!并非每个操作都必须在单个 SQL 查询中完成!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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