我的问题有点复杂。但我会在下面详细解释细节。
首先。我有三个数据库表,即公司,项目和阶段。当我运行sql:
SELECT company.ID AS companyID,company.Name AS companyName,project.ID AS projectID,project.Name AS projectName,phase.ID AS phaseID,phase.Name AS phaseName FROM company left join project ON project.CompanyID = company .ID左连接阶段ON阶段.ProjectID = project.ID WHERE company.Name =' Company2'按项目排序.ID
但是现在我在下面的图片中添加了一个新的数据库表,即treeview_items。
我希望让treeview_items表加入表公司,表项目和表阶段,使用phase.Name = treeview_items.text WHERE company.Name = Company2
我尝试了很多方法,但也无法达到我想要的效果。谁知道如何解决这个问题请帮帮我。非常感谢!!!
完整的treeview_items表
CREATE TABLE treeview_items(id int(11)NOT NULL,text varchar(200)NOT NULL,parent_id varchar(200)NOT NULL)ENGINE = InnoDB DEFAULT CHARSET = latin1;
INSERT INTO treeview_items(id,text,parent_id)VALUES(1,' Select All',' 0'),(5,' Company1',& #39; 1'),(7,'公司2',' 1'),(9,' Project11',' 5' ;),(10,' Project23',' 7'),(12,' Project4',' 7'),(14, ' Project5',' 7'),(16,' ProjectTest1',' 7'),(24,' Phase1&# 39;,' 9'),(25,'第2阶段',' 9'),(26,'第3阶段',' ; 9'),(27,' Phase4',' 9'),(29,' PhaseTest7',' 9') ,(30,' phase23',' 10'),(31,' Phase1',' 12'),(32,&# 39; PhaseTest6',' 14'),(33,' PhaseTest5',' 16'),(34,' PhaseTest4' ,' 16'),(56,' CompanyTest1',' 1'),(63,' ProjectTest7',' 56& #39;),(64,' PhaseTest3',' 63'),(65,' Phase1',' 10'),( 68,' PhaseTest2',' 63'),(69,' PhaseTest1&#39 ;,' 63'),(70,' Phase2',' 10'),(73,' PhaseTest2',' 16&#39);
答案 0 :(得分:0)
尝试此查询
SELECT company.ID AS companyID, company.Name AS companyName, project.ID AS projectID, project.Name AS projectName,phase.ID AS phaseID, phase.Name AS phaseName, tv.id as treeview_items_id FROM company left join project ON project.CompanyID = company.ID left join phase ON phase.ProjectID = project.ID left join treeview_items tv on(tv.text = phase.Name) WHERE company.Name = 'Company2' group by phaseID Order By project.ID