2016-10-11 86 views
0

我有一個包含用戶代理的日常計數打我的服務器表:如何通過Zend框架像JOIN 2

browser_stats_daily 
+----+-------------+----------------------------------------------------------------------+--------------+ 
| id | access_date | user_agent               | num_requests | 
+----+-------------+----------------------------------------------------------------------+--------------+ 
| 6 | 2016-09-24 | Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)  |   4729 | 
| 10 | 2016-09-24 | Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko  |  16396 | 
| 12 | 2016-09-24 | Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko |  33623 | 
| 17 | 2016-09-24 | MobileSafari/602.1 CFNetwork/808.0.2 Darwin/16.0.0     |   98 | 
| 28 | 2016-09-24 | Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)  |  10333 | 
| 33 | 2016-09-24 | Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko |   5745 | 
| 34 | 2016-09-24 | Mozilla/5.0 (compatible; AhrefsBot/5.1; +http://ahrefs.com/robot/) |   5 | 
| 46 | 2016-09-24 | Mozilla/5.0 (Windows NT 6.1; rv:49.0) Gecko/20100101 Firefox/49.0 |   339 | 
| 51 | 2016-09-24 | -                 |   13 | 
| 53 | 2016-09-24 | MobileSafari/601.1 CFNetwork/758.5.3 Darwin/15.6.0     |   38 | 
+----+-------------+----------------------------------------------------------------------+--------------+ 

我試圖產生主要/次要瀏覽器版本的一個簡單的報告 - 這一點,我已經用戶代理的表匹配:

user_agents 
+----+----------------+---------+----------+-------+-------+ 
| id | user_agent  | vendor | platform | major | minor | 
+----+----------------+---------+----------+-------+-------+ 
| 2 | %Firefox/38.0% | Mozilla | Firefox | 38 | 38.0 | 
+----+----------------+---------+----------+-------+-------+ 

我展示這可以通過查詢工作:

select distinct(bsa.user_agent), ua.vendor 
from user_agents as ua 
right join browser_stats_daily as bsa 
on bsa.user_agent LIKE ua.user_agent 
where ua.vendor is not null limit 10; 

將返回:

+----------------------------------------------------------------------------------------+---------+ 
| user_agent                    | vendor | 
+----------------------------------------------------------------------------------------+---------+ 
| Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0    | Mozilla | 
| Mozilla/5.0 (Windows NT 5.1; rv:38.0) Gecko/20100101 Firefox/38.0      | Mozilla | 
| Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0    | Mozilla | 
| Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0    | Mozilla | 
| Mozilla/5.0 (Windows NT 6.1; rv:38.0) Gecko/20100101 Firefox/38.0      | Mozilla | 
| Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/38.0 Firefox/38.0; ADSSO   | Mozilla | 
| Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:38.0) Gecko/20100101 Firefox/38.0  | Mozilla | 
| Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:38.0) Gecko/20100101 Firefox/38.0  | Mozilla | 
+----------------------------------------------------------------------------------------+---------+ 

但是當我嘗試使用過Zend Framework的SQL TableGateway該查詢,我得到了LIKE的錯誤:

SQLSTATE[42000]: Syntax error or access violation: 
1064 You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use near 
'`LIKE` `user_agents`.`user_agent` WHERE `user_agents`.`user_agent` IS NOT NULL' 

下面是我使用建代碼查詢:

$select = $this->tableGateway->getSql()->select(); 

$select->quantifier(Select::QUANTIFIER_DISTINCT); 

$select->columns(array(
    "user_agent", 
)); 

$select->join(
    "browser_stats_daily", 
    "browser_stats_daily.user_agent LIKE user_agents.user_agent", 
    Select::SQL_STAR, 
    Select::JOIN_RIGHT 
); 

$select->where->isNotNull("user_agents.user_agent"); 

return $this->tableGateway->selectWith($select); 

回答

1

這是可能的用ON子句作爲ExpressionInterface:

$on = new Literal('browser_stats_daily.user_agent LIKE user_agents.user_agent'); 
$select = $this->tableGateway->getSql()->select(); 
$select->quantifier(Select::QUANTIFIER_DISTINCT) 
     ->columns(["user_agent"]) 
     ->join("browser_stats_daily", $on, Select::SQL_STAR, Select::JOIN_RIGHT) 
     ->where->isNotNull("user_agents.user_agent"); 
+0

謝謝 - API文檔只說'string'被'$ on'參數接受,並且我無法通過google找到任何東西。 – HorusKol