javascript - 如何在 ORDER BY 和限制之后动态添加查询参数
问题描述
我正在开发一个需要动态向查询添加参数的项目。我的查询是这样的:
let query2 = "SELECT al.*,CONVERT_TZ(al.date_created,'+00:00',?) as date_created, user.first_name as agent_name FROM `account_log` as `al` inner join `user` on user.id = al.user_id WHERE `al`.`account_id` = ? ORDER BY `al`.`id` DESC limit ?,?";
let params2 = [tz_offset,account_id, offset, limit];
我想在下面做:
query2 += " AND `event_type` = ? ";
params2.push(filter.event_type);
我怎样才能做到这一点?
解决方案
查询参数必须在 ORDER BY 和 LIMIT 之前。
首先添加您的动态参数,然后添加排序和限制。
let query2 = "SELECT al.*,CONVERT_TZ(al.date_created,'+00:00',?) as date_created, user.first_name as agent_name FROM `account_log` as `al` inner join `user` on user.id = al.user_id WHERE `al`.`account_id` = ? ";
let orderAndLimit = "ORDER BY `al`.`id` DESC limit ?,?";
let params2 = [tz_offset,account_id];
let orderAndLimitParams = [offset, limit]
query2 += " AND `event_type` = ? ";
params2.push(filter.event_type);
query2 += orderAndLimit;
params2.push(orderAndLimitParams);
推荐阅读
- xml - 在 XSLT 中查找具有最大子节点数的节点
- jquery - 使用 Ajax.js 和 JQuery.js 将多个文件发布为 null
- mysql - 在 Where 子句中使用 Join 的 Mysql 更新表
- android - 如何设置android web view内容的最大长度
- python-3.x - Tensorflow v1.10:将图像存储为字节字符串还是每个通道?
- reactjs - React Native - 在导航中的屏幕之间移动错误
- javascript - IOS全屏html5视频的错误
- asp.net-mvc - 将 api 与 asp.net MVC 一起使用
- java - 为什么 libgdx 正在填充堆?
- azure - Azure webjob 无法连接到存储帐户