如何在zend db中编写这些查询

时间:2011-11-11 08:46:49

标签: php

select test.*, 
       user.*,
       count(test_stats.test_public_id) as past_day_views,
       test_stats.test_public_id   
  from test 
  join user
  join test_stats
 where test.user_id=user.user_id 
   and test.test_public_id=test_stats.test_public_id
   and test_stats.updated_on > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
 group by test_stats.test_public_id
 order by title

select count(test_public_id) as total_views
  from test_stats
 where test_public_id ='$tourid'

上周视图的这些查询检索,因为我没有太多关于再次询问此查询 请帮帮我

select count(ms.test_public_id) as total_views, ms1.recent_views from test_stats ms
join (select count(test_stats.test_public_id) as recent_views from test_stats
where test_stats.test_public_id = '$tourid' and test_stats.updated_on > DATE_SUB(CURDATE(), INTERVAL 7 DAY)) ms1 where ms.test_public_id ='$tourid'

SELECT * FROM user WHERE activation = '0' AND user_id >1

select * from test where user_id ='$user_id' and test.url is not null

1 个答案:

答案 0 :(得分:0)

你的JOINS / WHERE根本不正确。我会解决这个问题。但是在你的模型中,创建一个执行此操作的函数:

class MyModel extends Zend_Db_Table_Abstract
{
    // ...
    public function getSomeData()
    {
        $cols = array(
            'test.*',
            'user.*',
            'count(test_stats.test_public_id) as past_day_views',
            'test_stats.test_public_id'
        );
        $select = $this->select()
            ->setIntegrityCheck(false)
            ->from('test', $cols)
            ->join('user', 'user.user_id = test.user_id', array())
            ->join('test_states', 'test_stats.test_public_id = test_stats.test_public_id', array())
            ->where('test_stats.updated_on > DATE_SUB(CURDATE(), INTERVAL 1 DAY)')
            ->group('test_stats.test_public_id')
            ->order('title');

        return $this->fetchAll($select);
    }

现在是第二个问题:

$select = $this->select()
    ->from('test_stats', array('count(test_public_id) AS total_views'))
    ->where('test_public_id = ?', $tourid);

return $this->fetchAll($select);