doutao1282 2016-06-10 11:29
浏览 46

从多个表中检索相应的和非对应的值

I have a following table structure:

Brands => Histories <= Users

Histories table contains following columns:

brandId, userId, points (for each brand how many points user scored points).. I need to sum all the points for user for which he scored in between two dates in history table.

The query would accept 4 parameters: userId, brandId, startDate, endDate... The following output would be something like:

userId        brandId       points

1               64           155 sum of all points in between two dates specified by the parameter. 

If the passed value of BrandId is = null (ie. not passed). Query should return as well whole data about the brand and user if it doesn't finds any data in history table on that BrandId and UserId... I have written my own query in Zend Framework 2 but it doesn't outputs that I want (returns only records that are actually found in history table)...

It looks like this:

public function BrandWallBrandsById($userId,$brandId,$startDate,$endDate)
{
    $select = new Select();
    $select->from(array('p' => $this->table), array('id'));
    $select->join(array('b' => 'brands'), 'p.brandId = b.id', array('id','name', 'cover', 'slogan', 'startDate', 'homepage','endDate','active'));
    $select->columns(array(new Expression('SUM(p.points) as points'), "userId", "brandId"));
    $select->order("p.points asc");
    $select->group("p.brandId");
    $where = new Where();
    $where->notEqualTo("p.points", 0);
    $where->notEqualTo("p.type",10);
    $where->equalTo("p.userId", $userId);
    $where->equalTo("p.brandId", $brandId);
    $where->equalTo("b.active",1);
    $where->between("p.time", $startDate, $endDate);
    $select->where($where);
    return $this->historyTable->selectWith($select)->toArray()[0];
}

Note that the query only returns single record at a time when its called.. I wanna write a pure SQL statement, since I think it might be easier than this... Can someone help me out?

  • 写回答

1条回答 默认 最新

  • douqiaotong8682 2016-06-10 13:03
    关注

    Here is a SQL statement to get you what you want in SQL Server, though i'm not familiar with zend-framework.

    declare @startDate datetime, @endDate datetime, @userId int = null, @brandId int = null
    set @startDate = '6/1/2016'
    set @endDate = '6/9/2016'
    set @userId = 1      --this can be NULL
    set @brandId = 64    --this can be NULL
    
    select
        userId,
        brandId, 
        sum(points) as points
    from
        historyTable
    where
        (@userId is null or userId = @userId)
        and (@brandId is null or brandId = @brandId)
        and dateColumn >= @startDate
        and dateColumn < dateadd(day,1,@endDate) 
    group by
        userId, brandId
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错