search array element in LIKE mysql -
i have stored procedure searches bellow:
begin #at first, search in name of job: (select * tb_job `name` '%some%' , `name` '%thing%') union # second, search tags: (select * tb_job id in ( select idjob ( (select 2 priority1, count(tb_job_tag.idtag) priority2, idjob tb_job_tag idtag in (select tb_tag.id tb_tag tag '%some%' or tag '%thing%') group tb_job_tag.idjob) union (select 1, count(tb_job_tag.idtag), idjob tb_job_tag idtag in (select tb_tag.id tb_tag tag '%some%' , tag '%thing%') group tb_job_tag.idjob) ) t order priority1, priority2 desc ) ) end
now have 2 questions: how can pass array of words , separate them in mysql , use them in like
? second, how can make search better?
(i have 3table: tb_job, tb_tag, tb_job_tag stores job's id , tag's id). help.
/** * http://www.aspdotnet-suresh.com/2013/07/sql-server-split-function-example-in.html */ create function dbo.array(@string nvarchar(4000), @delimiter char(1)) returns @results table ([id] [bigint] identity(1,1) not null, items nvarchar(4000)) begin declare @index int declare @slice nvarchar(4000) -- have set 1 doesnt equal z -- ero first time in loop select @index = 1 while @index !=0 begin -- index of first occurence of split character select @index = charindex(@delimiter,@string) -- push left of slice variable if @index !=0 select @slice = left(@string,@index - 1) else select @slice = @string -- put item results set insert @results(items) values(@slice) -- chop item removed off main string select @string = right(@string,len(@string) - @index) -- break out if done if len(@string) = 0 break end return end
execute function once in database , onward access specific value can write query below
declare @value varchar(100); select top 1 @value = items [dbo].[array] ('some,thing,like,that' , ',') id = 2 print @value
all need change id in select statement. it'll accept string values now. can convert string int in sql using cast
i created function in hurry if have suggestions/modifications let me know...
Comments
Post a Comment