所以我有一個表與所有用戶信息和另一個表與產品信息。一表多個字段到其他表
在每個產品上都有對多個用戶的引用。 例如:user1是生產者,user2是productSupervisor,user3是productManager。
我怎樣才能得到的信息是這樣的: product1.producer是User1.Name,product1.supervisor是user2.Name和product.manager是user3.Name
生產,productSupervisor和productManager存儲爲用戶ID。
創建報表
CREATE TABLE employee (
idEmployee int(11) NOT NULL AUTO_INCREMENT,
networkID varchar(45) NOT NULL,
firstName varchar(100) NOT NULL,
lastName varchar(100) NOT NULL,
phoneExtension varchar(10) DEFAULT NULL,
email varchar(50) NOT NULL,
mobile varchar(20) DEFAULT NULL,
onCall tinyint(4) NOT NULL DEFAULT '0',
lastLogin varchar(100) DEFAULT 'Never',
active tinyint(4) NOT NULL DEFAULT '1',
createDate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (idEmployee),
KEY idx_employee (idTeam),
KEY idx_employee_0 (idPosition),
CONSTRAINT fk_employee_employeepositions FOREIGN KEY (idPosition) REFERENCES employeePositions (idPosition) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_employee_teams FOREIGN KEY (idTeam) REFERENCES teams (idTeam) ON UPDATE NO ACTION
)
CREATE TABLE product (
idProduct int(11) NOT NULL,
idSupervisor int(11) NOT NULL DEFAULT '1',
idProducer int(11) NOT NULL DEFAULT '1',
idManager int(11) NOT NULL DEFAULT '1',
prodcutInfo longtext,
PRIMARY KEY idProduct
)
'選擇的product_id,user1.name,user2.name,用戶3。名稱 左加入用戶在product.user1_id = users.user_id用戶1 左加入用戶在product.user2_id = users.user_id用戶2 左加入用戶在product.user3_id = users.user_id用戶3 其中product_id =?' –
或者 - 如@ryanVincent所說,如果我們知道查詢實際應該是什麼樣子,它會容易得多:) –
我還沒有插入任何數據....我我仍然試圖將我的頭圍繞在結構上......以瞭解是否需要改變結構。但我找不到一種方法讓查詢變得有意義。 – pmoreira