标题比较绕口,我也不知道该怎样表达才好。大概意思就是:thinkphp 中的SQL语句查询,用到数组作为查询条件后,如何添加比较字段查询。 thinkphp手册上,推荐使用数组形式来作查询,这样安全性很高。但是我却遇到一个问题,当用数组形式来进行比较,就无法进
标题比较绕口,我也不知道该怎样表达才好。大概意思就是:thinkphp 中的SQL语句查询,用到数组作为查询条件后,如何添加比较字段查询。
thinkphp手册上,推荐使用数组形式来作查询,这样安全性很高。但是我却遇到一个问题,当用数组形式来进行比较,就无法进行字段比较了,比较郁闷。代码如下:
首先,我需要的SQL语句是这样的。
select * from post where (id = '100') and (user_id = `user`.user_id)
在TP用数组写法是这样的
$sql = M('post') $map['id']??=?array('eq',100); $map['user_id']??=?array('eq','user.user_id'); $sql->where($map)->$sql;
生存的SQL语句会这样:
select * from post where (id = '100') and (user_id = 'user.user_id')
后部分的字段比较,因为多了单引号,变成一个比较字符串了。不是我想要的语句。
翻篇了thinkphp官网手册也没有找到解决方案,谷歌百度也没有找到。后来灵机一动,不清楚where是否可以重复写多次两次?将代码改成如下:
$sql = M('post') $map['id']??=?array('eq',100); $sql->where($map)->where('user_id = user.user_id')->$sql;
执行$sql->getlastsql();打印获得以下SQL:
select * from post where (id = '100') and (user_id = `user`.user_id)
噢!这正是我需要的SQL语句。看来where的确是可以重复调用的。
最后吐槽一下thinkphp手册,上面很多知识点都没有提及到。看着真的点有辛苦。还好这点辛苦是值得的。现在thinkphp用着很舒服。
原文地址:thinkphp复杂查询之数组混入字段比较, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
