SQL加入4个表,仅选择2

时间:2018-01-07 21:04:14

标签: mysql sql

CREATE TABLE user (
userID INT AUTO_INCREMENT PRIMARY KEY,
accountTypeID INT NOT NULL,
userEmail VARCHAR(320) NOT NULL,
userPassword VARCHAR(32) NOT NULL,
userFirst VARCHAR(20) NOT NULL,
userLast VARCHAR(20) NOT NULL,
userPhone VARCHAR(10) NOT NULL,
lastAccess TIMESTAMP NOT NULL,
userRegistry DATE NOT NULL,
FOREIGN KEY (accountTypeID) REFERENCES accountType(accountTypeID));


CREATE TABLE participant (
participantID INT AUTO_INCREMENT PRIMARY KEY,
vehicleID INT NOT NULL,
userID INT NOT NULL,
eventID INT NOT NULL,
participantRegistry DATE NOT NULL,
paymentMethodID INT NOT NULL,
FOREIGN KEY (vehicleID) REFERENCES vehicle(vehicleID),
FOREIGN KEY (userID) REFERENCES user(userID),
FOREIGN KEY (eventID) REFERENCES event(eventID),
FOREIGN KEY (paymentMethodID) REFERENCES paymentMethod(paymentMethodID));


CREATE TABLE vehicle (
vehicleID INT AUTO_INCREMENT PRIMARY KEY,
makeID INT NOT NULL,
vehicleModel VARCHAR(20) NOT NULL,
vehicleYear VARCHAR(4) NOT NULL,
tshirtID INT NOT NULL,
clubID INT NOT NULL,
FOREIGN KEY (makeID) REFERENCES make(makeID),
FOREIGN KEY (tshirtID) REFERENCES tshirt(tshirtID),
FOREIGN KEY (clubID) REFERENCES club(clubID));


CREATE TABLE club (
clubID INT AUTO_INCREMENT PRIMARY KEY,
clubName VARCHAR(20) NOT NULL,
clubLogo BLOB);


$sql="SELECT U.userFirst, U.userLast, C.clubName FROM user AS U, club AS C
   INNER JOIN participant AS P
        on P.userID = U.userID
   INNER JOIN vehicle AS V
        on V.vehicleID = P.vehicleID
   INNER JOIN club AS C
        on C.clubID = V.clubID
   ORDER BY C.clubName ASC";

我们正试图让每个俱乐部的所有俱乐部和所有成员(名字和姓氏)。

仅需要参与者和车辆表将用户表链接到俱乐部表。

我知道这很简单,我们使用的服务器不显示sql错误,也没有phpmyadmin。

1 个答案:

答案 0 :(得分:0)

感谢maSTAShuFu,我来到了工作结果。

I'm row number 1