table1
---+---------
id | value
---+---------
1 | (NULL)
2 | 'string'
3 | (NULL)
table2
---+-----------
id | table1_id
---+-----------
1 | 3
我可以通过执行
来获得我的结果select table1.id from table1 where table1.value is not null
union
select table1.id from table1 right join table2 on table1.id=table2.table1_id
所以我需要得到的是
---+--
id |
---+--
2 |
3 |
但是我不能使用union,因为我应该使用不支持union的yii1.1 CDbCriteria 我尝试了不同的连接类型但没有结果。
答案 0 :(得分:2)
您可以使用LEFT JOIN以这种方式编写查询:
SELECT table1.id
FROM
table1 LEFT JOIN table2
ON table1.id=table2.table1_id
WHERE
table1.value IS NOT NULL
or table2.table1_id IS NOT NULL
因为你正在使用LEFT JOIN,所以当连接不成功时,table2.table1_id将为null。
答案 1 :(得分:0)
为什么你需要一个连接,这个简单的查询应该这样做:
从table1中选择id,其中value不为null或id in(select 表2中的distinct table1_id