我正在尝试提高我对 SQL 查询的了解,但我陷入了多对多关系查询。
使用PHP和MySQL的数据库结构如下:
数据库结构:
--------------------------------------------
| colour |
--------------------------------------------
| colour_id | colour |
--------------------------------------------
| 1 | blue |
--------------------------------------------
| 2 | red |
--------------------------------------------
############################################
--------------------------------------------
.
-----------------------------------------------------------------
| product_colours |
-----------------------------------------------------------------
| id | product_id | colour_id |
-----------------------------------------------------------------
| 1 | 1 | 2 |
-----------------------------------------------------------------
| 2 | 2 | 1 |
-----------------------------------------------------------------
#################################################################
-----------------------------------------------------------------
主表
-----------------------------------------------------------------
| 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 |
--------------------------------------------
############################################
--------------------------------------------
我可以使用连接写一个查询,它说:SELECT * colours WHERE group_name = product_group_1
?
任何帮助都会很棒。太感谢了。