【发布时间】:2014-12-09 05:14:56
【问题描述】:
我目前有使用查询生成器而不是 Eloquent 从我的数据库中获取一些统计信息的功能。
假设我想计算拥有 iOS 令牌的用户数量,然后是在我的一张表中拥有 android 令牌的用户。
class Connection {
public function __construct()
{
return $this->db = DB::connection('database_one');
}
}
class Statistics extends Connection {
public function devices()
{
$this->devices = $this->db->table('users_devices');
$arr = [
'ios' => $this->devices->where('ios_push_token', '!=', '')->count(),
'android' => $this->devices->where('android_push_token', '!=', '')->count(),
];
return $arr;
}
}
获取ios设备的查询正确:
select count(*) as aggregate from `users_devices` where `ios_push_token` != ?
array (size=1)
0 => string '' (length=0)
但是,然后我遇到了 android 值的问题,查询尝试执行:
select count(*) as aggregate from `users_devices` where `ios_push_token` != ? and `android_push_token` != ?
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
似乎将第一个查询中的 where 子句链接到第二个查询上,以此类推,这给了我不正确的数据。
我认为这与使用DB::connection 的一个实例有关,但我不确定?
【问题讨论】:
-
只使用有意义的名称。
$this->devices不是devices,而是Query\Builder实例,因此显然您需要 2 个这样的实例来进行单独的查询。
标签: php mysql sql laravel laravel-4