我正在尝试使用相同的表数据(即用户)设置双向多对多关系,我还有一个名为 Userusers 的链接表,它将一个用户与另一个用户连接起来,但我不确定如何处理双向端,因为我的代码的结果只显示了一个方向。
--Table User:
create table User (
UserID int auto_increment not null,
UserFirstName varchar(30) not null,
UserSurname varchar(30) not null,
UserTel char(10),
UserCell char(10),
UserEmail varchar(50) not null,
UserPassword varchar(50) not null,
UserImage varchar(50),
UserAddress1 varchar(50),
UserAddress2 varchar(50),
UserTown/City varchar(50),
UserProvince varchar(50),
UserCountry varchar(50),
UserPostalCode varchar(50),
Primary key(UserID)
)
--Table Userusers:
create table UserUsers (
UserID int not null,
FriendID int not null,
primary key(UserID, FriendID),
foreign key(UserID) references User(UserID),
foreign key(FriendID) references User(UserID)
)
PHP代码:
$sql="SELECT * FROM User u INNER JOIN UserUsers uu ON uu.UserID = u.UserID INNER JOIN User f ON f.UserID = uu.FriendID WHERE uu.UserID = " . $_SESSION['userID'];