我有两张桌子
Activity
ID | user_type | user_id | status
1 | PROFESSIONAL | 15 | Hello
2 | VENDOR | 15 | Hi
3 | PROFESSIONAL | 16 | My status
4 | PROFESSIONAL | 18 | Abcd
Followers
ID | user_type | user_id | follower_type | follower_id
1 | PROFESSIONAL | 15 | PROFESSIONAL | 16
2 | VENDOR | 15 | PROFESSIONAL | 16
3 | PROFESSIONAL | 20 | PROFESSIONAL | 17
我希望获得PROFESSIONAL
id
16
的所有活动以及PROFESSIONAL
id
16
所有用户的活动以下。
所以我应该得到结果 -
Activity
ID | user_type | user_id | status
1 | PROFESSIONAL | 15 | Hello
2 | VENDOR | 15 | Hi
3 | PROFESSIONAL | 16 | My status
我不确定如何在单个查询中获取它。另外,我使用的是Yii 1.1。所以我想用Yii模型获取记录。在构建MYSQL查询或使用Yii CActiveRecord获得所需结果方面的任何帮助都会很好。
答案 0 :(得分:0)
确定。所以现在,我这样做如下
$criteria = new CDbCriteria;
$criteria->select = 't.*';
$criteria->join = 'LEFT JOIN followers AS f ON (t.user_id = f.user_id AND t.user_type = f.user_type)
WHERE (f.follower_id='.$user_id.' AND f.follower_type = "'.$user_type.'")
OR (t.user_id='.$user_id.' AND t.user_type = "'.$user_type.'")';
我很想知道是否有更好的解决方案。