简体   繁体   中英

MySQL and PHP Many-to-Many Database Relationships

I'm trying to advance my knowledge of SQL queries but am getting stuck on a many-to-many relationship query.

Using PHP and MySQL database structure is as follows:

Database structure:

--------------------------------------------
|                  colour                  |
--------------------------------------------
| colour_id         |      colour          |
--------------------------------------------
| 1                 |      blue            |
--------------------------------------------
| 2                 |      red             |
--------------------------------------------
############################################
--------------------------------------------

.

-----------------------------------------------------------------
|   product_colours                                             |
-----------------------------------------------------------------
| id            |     product_id       |    colour_id           |
-----------------------------------------------------------------
| 1             |         1            |         2              |
-----------------------------------------------------------------
| 2             |         2            |         1              |
-----------------------------------------------------------------
#################################################################
-----------------------------------------------------------------

MAIN TABLE

-----------------------------------------------------------------
|   products                                                    |
-----------------------------------------------------------------
| id          |      name               |      details          |
-----------------------------------------------------------------
| 1           |      product 1          |      blah             |
-----------------------------------------------------------------
| 2           |      product 2          |      blah             |
-----------------------------------------------------------------
#################################################################
-----------------------------------------------------------------


-----------------------------------------------------------------
|    product_group_names                                        |
-----------------------------------------------------------------
| id            |      product_id         |    group_name_id    |
-----------------------------------------------------------------
| 1             |          1              |         1           |
-----------------------------------------------------------------
| 2             |          2              |         2           |
-----------------------------------------------------------------
#################################################################
-----------------------------------------------------------------


--------------------------------------------
|        group_name                        |
--------------------------------------------
| group_name_id     |      group_name      |
--------------------------------------------
| 1                 |  product_group_1     |
--------------------------------------------
| 2                 |  product_group_2     |
--------------------------------------------
############################################
--------------------------------------------

Could I write one query using joins which says: SELECT * colours WHERE group_name = product_group_1 ?

Any help would be amazing. Thank you so much.

You can certainly write a query to do that. You'll need to basically join your tables together so you can link the data you need from table to table, and then add a where clause for your desired condition.

Your colours are "linked" to your product_colours via the colour_id attribute, the product colours are linked to the product_group_names via the product_id attributes on both tables, and your group names are linked via the group_name_id and group_id attributes on the group_names table.

When you're doing this kind of query though, if you have a lot of records in these tables - the queries can go very slow. You need to make sure you add indicies on each of the columns referenced in the JOIN...ON clauses, as well as the WHERE clause.

SELECT colour FROM colours c
JOIN product_colours pc ON c.id = pc.colour_id
JOIN product_group_names pgn ON pgn.product_id = pc.product_id
JOIN group_name gn ON gn.id = pgn.group_name_id
WHERE gn.group_name = "product_group_1"

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