我有一张桌子,上面有人们假期' id',' day',' month',' name'
<table rules='all' border='black'>
<tr>
<td>id</td>
<td>day</td>
<td>month</td>
<td>name</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>5</td>
<td>Adam</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>5</td>
<td>Pavel a Petr</td>
</tr>
</table>
&#13;
strtolower($name) => $name = Pavel a Petr
我有一个名为Pavel
现在我需要可以select * from users where lower(name) contains '".$name."'
我正在寻找SQL解决方案。 如果没有SQL解决方案,可以有人建议我使用php解决方案。
谢谢。
答案 0 :(得分:3)
E.g:
"select * from `users` where lower(name) LIKE '%$name%'"
如果SQL LIKE子句与%字符一起使用,那么它将在UNIX中像元字符(*)一样工作,同时在命令提示符下列出所有文件或目录。
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以使用REGEXP
SELECT * FROM `users` WHERE LOWER(name) RLIKE "[[:<:]]$name[[:>:]]"
答案 3 :(得分:0)
也许这就是你要找的:
编辑:您不应该让用户名为&#34; a&#34; : - )
EDIT2:刚刚发现,sql-server标签被带走......
DECLARE @peopleHolidays TABLE(id INT,day INT,month INT, name VARCHAR(100));
INSERT INTO @peopleHolidays VALUES(1,1,5,'Adam'),(2,2,5,'Pavel a Petr'),(2,3,5,'Adam a Max'),(2,4,5,'Max a Petr');
DECLARE @users TABLE(id INT IDENTITY,name VARCHAR(100));
INSERT INTO @users VALUES('Adam'),('Pavel'),('Petr'),('Max');
SELECT *
FROM @peopleHolidays AS pH
CROSS APPLY(SELECT CAST('<r>' + REPLACE(pH.name,' ','</r><r>') + '</r>' AS XML)) AS AsXml(x)
CROSS APPLY
(
SELECT * FROM @users AS u
WHERE u.name IN
(
SELECT a.b.value('.','varchar(max)')
FROM AsXml.x.nodes('/r') AS a(b)
)
) AS UsersInHoliday
答案 4 :(得分:0)
正如其他海报所说,LIKE会为你提供部分字符串的比较器。您可以对like参数使用字符串连接,因此可以安全地传递您正在搜索的字符串,而不会产生SQL注入的风险
select * from users where name like '%' + :name + '%'
在PHP中使用预准备语句;
答案 5 :(得分:0)
所以我通过PHP解决了这个问题,而不是SQL,代码看起来像是
function isClientInDb($name, $array){
global $db;
$name = strtolower($name);
$query = "SELECT * from `clients` where LOWER (name) = '".$name."' and `uid`='".$_SESSION['uid']."'";
$result = $db->select($query);
if(count($result) < 1){
if(intval(preg_match('/\s/',$name)) > 0){
if(strpos($name, ",")){
$pos = strpos($name, ",");
$minus = 1;
}else{
if(strpos($name, "a")){
$pos = strpos($name, "a");
$minus = 0;
}else{
$pos = strpos($name, " ");
}
}
$pole = array(strtolower(substr($name,0,$pos)), strtolower(substr($name,-($pos+$minus))));
$sql = "lower(name) = '".$pole[0]."' or lower(name) = '".$pole[1]."'";
}else{
$sql = "lower(name) LIKE '%".$name."%'";
}
$query = "SELECT * from `clients` where ( $sql ) and `uid`='".$_SESSION['uid']."'";
$result = $db->select($query);
if(count($result) < 1){
return false;
}else{
return true;
}
}else{
return true;
}
}