我有一个表,其中包含每天命中服务器的用户代理的数量:

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);

最佳答案

使用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");

10-05 20:29