假设我有两个表A和表B
表A
id name remarks
4 X XXX
6 Y YYY
7 Z ZZZ
表B
id Aid remarks edit_flag
1 4 NULL 0
2 6 YY changes 1
3 7 Z cahnged 1
所以,我想检索如下数据:
如果edit_flag为1(即编辑),则从表B获取备注列,否则(edit_flag为0)从表A获取备注列,因为它未被编辑
我正在看这样的事情
if(edit_flag == 0)
then get remarks from table A
else get remarks from table B
所以我的结果表应该看起来像
Row_Counter remarks
1 XXX
2 YY changes
3 Z changed
答案 0 :(得分:4)
使用CASE
:
SELECT aID = a.id, name,
remarks = CASE b.edit_flag
WHEN 0 THEN a.remarks
WHEN 1 THEN b.remarks
END
FROM TableA a INNER JOIN TableB b ON a.id = b.Aid
答案 1 :(得分:0)
您可以加入2个表并进行条件查询。如:
注意:我假设您正在使用支持" iif"的数据库。否则你应该使用" case"和其他答案一样。
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.alpha = 0
let rotation = CATransform3DTranslate(CATransform3DIdentity, -250, 20, 0)
cell.layer.transform = rotation
UIView.animateWithDuration(0.9){
cell.alpha = 1
cell.layer.transform = CATransform3DIdentity
}
答案 2 :(得分:0)
使用案例陈述
declare @tblA as table
(
id int,
name varchar(50),
remarks varchar(50)
)
insert into @tblA values(4,'x','xxx');
insert into @tblA values(6,'y','yyy');
insert into @tblA values(7,'z','zzz');
declare @tblB as table
(
id int,
Aid int,
remarks varchar(50),
edit_flag int
)
insert into @tblB values(1,4,NULL,0);
insert into @tblB values(2,6,'yy changes',1);
insert into @tblB values(3,7,'z changes',1);
SELECT
B.id,
B.Aid,
B.edit_flag,
CASE WHEN edit_flag=1 THEN B.remarks ELSE a.remarks END as remarks
FROM @tblB B
LEFT JOIN @tblA A ON B.Aid=A.id