内连接但我不理解另一个表的外键位

时间:2014-01-10 18:14:02

标签: php mysql

我有4张这样的表

产品表:

+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| productID    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name         | varchar(45) | YES  |     | NULL    |                |
| price        | int(11)     | YES  |     | NULL    |                |
| manufacturer | varchar(45) | YES  |     | NULL    |                |
| rating       | int(2)      | YES  |     | NULL    |                |
| categoryID   | int(11)     | YES  | MUL | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+

类别表:

+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| categoryID   | int(11)     | NO   | PRI | NULL    | auto_increment |
| categoryName | varchar(64) | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+

用户:

+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| userID    | int(11)     | NO   | PRI | NULL    | auto_increment |
| firstname | varchar(45) | YES  |     | NULL    |                |
| age       | int(11)     | YES  |     | NULL    |                |
| location  | varchar(45) | YES  |     | NULL    |                |
| username  | varchar(45) | YES  |     | NULL    |                |
| password  | varchar(45) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+

修改:

+------------+------------+------+-----+---------+----------------+
| Field      | Type       | Null | Key | Default | Extra          |
+------------+------------+------+-----+---------+----------------+
| reviewID   | int(11)    | NO   | PRI | NULL    | auto_increment |
| reviewTime | date       | YES  |     | NULL    |                |
| reviewData | mediumtext | YES  |     | NULL    |                |
| productID  | int(11)    | YES  | MUL | NULL    |                |
| userID     | int(11)    | YES  | MUL | NULL    |                |
+------------+------------+------+-----+---------+----------------+

我基本上需要一个显示以下

的select语句
reviewID - reviewTime - reviewData - product.name - category.name - user.firstname

到目前为止,我已经提出了以下内容

SELECT r.reviewID, r.reviewTime, r.reviewData, p.name, u.firstname 
  FROM review r 
  INNER JOIN product p on p.productID = r.productID 
  INNER JOIN user u on u.userID = r.userID;

这给了我除了类别名称之外的所有东西,就像我被困住的那样。

数据库是这样链接的;

enter image description here

我只是不明白如何将categoryname从类别表传递到评论表

4 个答案:

答案 0 :(得分:3)

select r.reviewID, r.reviewTime, r.reviewData, p.name, u.firstname, c.categoryName
from review r
left join product p on p.productID = r.productID
left join user u on u.userID = r.userID
left join category c on c.categoryID = p.categoryID

注意:根据您的架构,我使用了LEFT JOIN,这样无论是否有相关数据,都会返回所有评论。

答案 1 :(得分:0)

您当前的疑问:

SELECT r.reviewID, r.reviewTime, r.reviewData, p.name, u.firstname 
FROM review r 
    INNER JOIN product p on p.productID = r.productID 
    INNER JOIN user u on u.userID = r.userID;

由于您已加入产品表并且产品表中引用了categoryID,因此只需添加其他联接:

SELECT 
    r.reviewID, r.reviewTime, r.reviewData, p.name, u.firstname, c.categoryName  
FROM review r 
    INNER JOIN product p on p.productID = r.productID 
    INNER JOIN user u on u.userID = r.userID;
    INNER JOIN category c ON c.categoryID = p.categoryID

答案 2 :(得分:0)

添加INNER JOIN category c on c.categoryID = p.categoryID

答案 3 :(得分:0)

您没有加入类别表添加

Join category c on c.categoryID = p.categoryID

并添加选择

 c.name

只有在表之间的任何链接可能不存在时,才将连接更改为左连接。