我有3张桌子。 (例如,我最初采用的是一些简短的名字)
block_id是所有三个表的连接属性
注释:
我想显示block_id,slab的总数,各个block_id的p_status,其中status ='cut'
我已经尝试了以下查询,但它不起作用,
select 1.block_id,sum(2.noofslabs) as totalslabs,if(3.status='completed','fullyCompleted','partiallyCompleted') from table1 as 1
left join table2 as 2 on 1.block_id=2.block_id
left join table3 as 3 on 1.block_id=3.block_id where 1.status='Cut'
如果我没有添加table3,则totalslabs的结果是正确的,如果我添加连接table3,结果是错误的。 还有一件事是如何将z_status作为部分完成或完全完成
答案 0 :(得分:0)
您不能将数字用作表别名。使用以字母开头的名称。
select t1.block_id,sum(t2.noofslabs) as totalslabs,if(t3.status='completed','fullyCompleted','partiallyCompleted') from table1 as t1
left join table2 as t2 on t1.block_id=t2.block_id
left join table3 as t3 on t1.block_id=t3.block_id where t1.status='Cut'
应该有效