Mysql根据第三个表从2个表中选择特定数据

时间:2017-04-28 11:21:56

标签: php mysql

我有三张桌子

tbl_project

db_projectid  db_projectname
1              test
2              xxx

tbl_activities

db_id db_projectid db_category
1      1            Civil Work
2      1            Mechanical
3      1            Electrical

tbl_dailypercentage

db_dpid  db_aid db_projectid db_status
1         1           1        red
2         1           1    
3         2           1

db_projectid tbl_project

中的主键

db_id 是主键, db_projectid tbl_activities

中的外键

db_dpid 是主键, db_aid tbl_dailypercentage 中的外键 我试过这个查询

select
activities.db_id,
activities.db_category as cat,
dailypercentage.db_status,
dailypercentage.db_aid,
dailypercentage.db_projectid
from tbl_activities as activities,tbl_dailypercentage as dailypercentage
where 
dailypercentage.db_projectid='$projectId'
and
activities.db_id=dailypercentage.db_aid
and 
dailypercentage.db_status='red'

但我有ab错误

  

未定义的变量:cat

while($row=mysqli_fetch_array($statusQuery)){
        $status=$row['db_status'];
        $cat=$row['cat'];
        }  
<td> <?php if($cat=="Civil Work" && $status="red"){
echo"<p style='color:$status'>".($sumcivil/$civilCount)."</p>";}
else{echo ($sumcivil/$civilCount);}?>
</td> 

我还尝试左连接和右连接

得到我想要的结果

我想要的结果是

对于ID = 1

的项目
category         status
civil work       red
mechanical work  
electrical work 

对于ID = 2

的项目
  category         status
  civil work      
  mechanical work  
  electrical work 

2 个答案:

答案 0 :(得分:1)

您展示了此代码:

while($row=mysqli_fetch_array($statusQuery)){
        $status=$row['db_status'];
        $cat=$row['cat'];
}

如果您的查询未返回任何行,则永远不会定义$status$cat。我想这就是出了什么问题。如果您使用像phpMyadmin这样的MySQL客户端运行该查询会发生什么?

同样,如果您的查询返回多行,$status$cat将仅捕获最后一行的值。

在没有行和多行的情况下计算出你想要发生的事情非常重要。

答案 1 :(得分:0)

仅基于您问题中的表格,我将以下架构放在一起(我没有为外键添加约束,因为这仅用于测试)。

create table tbl_project (
    `db_projectid` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `db_projectname` VARCHAR(255) NOT NULL DEFAULT 'Undefined',
    PRIMARY KEY (`db_projectid`)
);

create table tbl_activities (
    `db_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `db_projectid` INT(1) NOT NULL,
    `db_category` VARCHAR(255) NOT NULL DEFAULT 'Undefined',
    PRIMARY KEY (`db_id`)
);
create table tbl_dailypercentage (
    `db_dpid` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `db_aid` INT(1) NOT NULL,
    `db_projectid` INT(1) NOT NULL,
    `db_status` VARCHAR(255) NULL,
    PRIMARY KEY (`db_dpid`)
);

insert into tbl_project (db_projectname) values ('Test'), ('xxx');
insert into tbl_activities (db_projectid, db_category) values ('1', 'Civil Work'), ('1', 'Mechanical'), ('1', 'Electrical');
insert into tbl_dailypercentage (db_aid, db_projectid, db_status) values
  ('1', '1', 'red'),
  ('1', '1', null),
  ('2', '1', null);


select a.db_category as cat, dp.db_status from tbl_project p
  left join tbl_activities a on p.db_projectid = a.db_projectid
  left join tbl_dailypercentage dp on dp.db_aid = a.db_id
  where p.db_projectid = 1
  group by a.db_id;

Results

我必须将group by添加到最终查询中,因为tbl_dailypercentage中的多行具有相同的db_aiddb_projectid值。

之后,你想要调整你的php代码,将td内容放在while循环中:

<?php while($row=mysqli_fetch_array($statusQuery)):?>
        $status=$row['db_status'];
        $cat=$row['cat'];
    <td> <?php if($cat=="Civil Work" && $status="red"){
        echo"<p style='color:$status'>".($sumcivil/$civilCount)."</p>";}
        else{echo ($sumcivil/$civilCount);}?>
    </td>
<?php endwhile; ?>