php - 如何通过 Zend Framework 2 加入 LIKE

标签 php mysql join zend-framework2 zend-db

我有一个表,其中包含访问我的服务器的用户代理的每日计数:

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

关于php - 如何通过 Zend Framework 2 加入 LIKE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39971418/

相关文章:

php - 如何确定下拉菜单中某个选项的优先级

mysql - 如何编写 HQL 查询

php - 带有 PHP PDO 的只读副本上的 MySQL 存储过程

php - 使用 php 和 MySql 的 html 下拉列表

sql - 试图将表格的最近行连接到另一个表格

python - 合并一个值介于两个其他值之间的 Pandas 数据框

C# 不喜欢 SQL JOINS

php - 未找到 Android 跟踪代码管理器,因此不会使用

php - 解码从 mysql 数据库中检索到的字符串

php - 可以使用一个或多个值进行搜索的搜索功能