任何人都可以为我解决问题吗?
我有一个最大长度的字符串数组。我想将所有字符串数组元素与单个SQL查询进行比较。我怎么能这样做?
<?php
//choices of tags, number of pictures, tag search based on given tags
//send back in Json format and decode json
$api_key = '4eb66ae95c7e8fb8dd729ebf61541d79';
if(isset($_POST['submit']))
{
$tag1 = htmlspecialchars($_POST['tag1']);
$tag2 = htmlspecialchars($_POST['tag2']);
$tag3 = htmlspecialchars($_POST['tag3']);
$tag = $tag1.",".$tag2.",".$tag3;
$perPage = 25;
$url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search';
$url.= '&api_key='.$api_key;
$url.= '&tags='.$tag;
$url.= '&per_page='.$perPage;
$url.= '&format=json';
$url.= '&nojsoncallback=1';
$response = json_decode(file_get_contents($url));
$photo_array = $response->photos->photo;
//check each photo from server, and specifiy url to print
foreach($photo_array as $single_photo)
{
$farm_id = $single_photo->farm;
$server_id = $single_photo->server;
$photo_id = $single_photo->id;
$secret_id = $single_photo->secret;
$size = 'm';
$title = $single_photo->title;
$photo_url = 'http://farm'.$farm_id.'.staticflickr.com/'.$server_id.'/'.$photo_id.'_'.$secret_id.'_'.$size.'.'.'jpg';
print "<img title='".$title."' src='".$photo_url."' />";
}
}
?>
字符串数组长度不固定,它是动态的。
例如:我的搜索字符串是“管理员登录错误”
然后我把它分成了
string[] new = searchtext;
select Qid from questions where qdescriptions like string[0],string[1],string[2]
分为三部分。我的预期结果应该包含数据库中的所有这三个字符串
喜欢这个
admin
login
error
希望你理解。结果应该包含单个搜索查询中的所有搜索字符串..
C#代码:
Admin post this;
password change for login;
the error database;
答案 0 :(得分:2)
试试这个
WITH CTE AS
(
SELECT jo.name,Pr1.job_ID, Pr1.run_date, Pr1.run_time,pr1.run_status,
Count(*) Over (Partition By Pr1.job_ID,Pr1.run_date,Pr1.run_time) As num,
Row_Number() Over (Partition By Pr1.job_ID,Pr1.run_date,Pr1.run_time
Order By pr1.run_date desc--, pr1.run_time desc
) As Rn
FROM MSDB.dbo.sysjobhistory Pr1
join MSDB.dbo.sysjobs jo on jo.job_id=pr1.job_id
JOIN MSDB.dbo.sysjobhistory Pr2
ON Pr1.job_ID = Pr2.job_ID --and pr1.run_status=pr2.run_status
and pr1.run_date >=pr2.run_date and pr1.run_time >=pr2.run_time
)
SELECT name,job_ID, run_date, run_time,run_status
FROM CTE
WHERE num <= 3 AND Rn = 1
创建一个功能
declare @searchtext nvarchar(max) = 'abc,def,pqr'
查询
CREATE FUNCTION [dbo].[fn_Split](@text varchar(8000), @delimiter varchar(20))
RETURNS @Strings TABLE
(
position int IDENTITY PRIMARY KEY,
value varchar(8000)
)
AS
BEGIN
DECLARE @index int
SET @index = -1
WHILE (LEN(@text) > 0)
BEGIN
SET @index = CHARINDEX(@delimiter , @text)
IF (@index = 0) AND (LEN(@text) > 0)
BEGIN
INSERT INTO @Strings VALUES (@text)
BREAK
END
IF (@index > 1)
BEGIN
INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))
SET @text = RIGHT(@text, (LEN(@text) - @index))
END
ELSE
SET @text = RIGHT(@text, (LEN(@text) - @index))
END
RETURN
END
答案 1 :(得分:0)
您可以通过动态创建SQL查询来执行此操作:
select * from yourtable y inner join (select value from
fn_split(@searchtext,',')) as split on y.qdescriptions like '%+split.value+%'
在应用程序中编写一个for循环遍历搜索数组的for循环: 伪代码传入:
string[] new = searchtext;
String query = "select Qid from questions";
注意:这是伪代码,因为你没有说你正在使用什么编程语言等我无法告诉你for循环的实际语法是什么样的,或者如何保护你的查询不受sqlInjection < / p>
您的C#代码应如下所示:
For(String searchstring in new){
if(new.indexof(searchstring) === 0){
query += " where qdescriptions like " + searchstring;
}
else{
//depending on what you want to do here use OR or AND
query += " or qdescriptions like " + searchstring;
}
}
result = query.execute();
答案 2 :(得分:0)
在C#中,您可以像这样生成查询文本......
public static void Main()
{
string final = GenerateParameters("tableName", "fieldName", new[] {"admin", "login", " error"});
// execute query
// final = "SELECT * FROM tableName WHERE fieldName LIKE '%admin%' OR fieldName LIKE '%login%' OR fieldName LIKE '% error%'"
}
static string GenerateParameters(string tableName, string fieldName, IEnumerable<string> searchTerms)
{
string sqlParameters = string.Join(" OR ", searchTerms.Select(x => "{0} LIKE '%{1}%'".FormatWith(fieldName, x)));
return "SELECT * FROM {0} WHERE ".FormatWith(tableName) + sqlParameters;
}
public static class StringExtensions
{
public static string FormatWith(this string value, params object[] args)
{
return String.Format(value, args);
}
}