简体   繁体   中英

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

I got a database table 'books', and a table 'reviews'. 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. Isn't this very performance and bandwidth inefficient? The server (NodeJS) needs to download alot of duplicate data. Is there another way to structure a hasmany relationship in MySQL, without downloading duplicate data? Or need I to switch to PostgreSQL for example?

If you don't want the duplication of values from the books table, then run two queries:

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!

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