Autosuggest search by full name - sql

I've implemented an autosuggest search using jQuery, php and SQL server 2008. I'm looking for people by their name. The name of the person is divided in three fields 'nombres, 'apellido_paterno' and 'apellido_materno'. My autosuggest matches results where one of the three fields looks like the pattern in the input text.
$values = array(':x'=>$data['term'].'%',':y'=>$data['term'].'%',':z'=>$data['term'].'%');
$sql = "SELECT TOP 10 id_persona, nombres +' '+ apellido_paterno +' '+ apellido_materno AS value
FROM personas WHERE nombres LIKE :x OR apellido_paterno LIKE :y OR apellido_materno LIKE :z";
So my query is working fine if you search by name or lastname, however if you search by full name there are no matches. So, how do I add criteria to my query in order to bring full name matches?

Assuming all three are varchar fields, and you are querying a somewhat limited number of records, I would just do:
$values = array(':x'=>'%'.$data['term'].'%');
$sql = "SELECT TOP 10 id_persona, nombres +' '+ apellido_paterno +' '+ apellido_materno AS value
FROM personas
WHERE
nombres + ' ' + apellido_paterno + ' ' + apellido_materno LIKE :x";

Related

How can I check the maximum value from a set of tables in SQL Server (if possible)?

We have a set of databases (80 in total). Every single one has a table called tblProfessions. The tables are not standardized. For example:
EDIT: all the databases are on the same server.
The DB1.dbo.tblProfessions is like:
intProfessionCode
strProfessionDescription
1
lawyer
2
dentist
...
...
30
doctor
And the DB72.dbo.tblProfessions is as follows:
intProfessionCode
strProfessionDescription
1
designer
2
butcher
...
...
80
chef
Suppose I ran a script from DBO1 to DBO72, and I found that the biggest table has 80 entries (in this case the DBO72 is the biggest one).
By my limited knowledge, all I know is to run the below script database by database, and write it down in a spreadsheet manually:
SELECT MAX(intProfessionCode) FROM [DB].dbo.tblProfessions;
Is there a script to run and loop through all the tblProfessions and get the one with the most entries? All I want is the biggest number found.
Thanks in advance.
You should be able to do something like this:
WITH dat
AS
(
SELECT 'db1' AS database_name, MAX(intProfessionCode) AS max_intProfessionCode
FROM DB1.dbo.tblProfessions
UNION ALL
...
UNION ALL
SELECT 'db72' AS database_name, MAX(intProfessionCode) AS max_intProfessionCode
FROM DB72.dbo.tblProfessions
)
SELECT dat.db, dat.max_intProfessionCode
FROM dat
INNER JOIN (SELECT MAX(max_intProfessionCode) AS max_intProfessionCode_overall
FROM dat) y
ON dat.max_intProfessionCode = y.max_intProfessionCode_overall
For situations like this, I usually query the catalog to write the above script rather than typing it out:
WITH
dat AS
(
SELECT STRING_AGG('SELECT ''' + QUOTENAME(s.name) + ''' AS db,
MAX(intProfessionCode) AS max_intProfessionCode
FROM ' + QUOTENAME(s.name) + '.' + QUOTENAME('dbo') + '.' + QUOTENAME('tblProfessions') + '
UNION ALL',' ') AS s
FROM sys.databases s
WHERE s.name LIKE 'DB%' --EDIT APPROPRIATELY
)
SELECT 'WITH dat
AS
(' + SUBSTRING(s,1,LEN(s) - LEN(' UNION ALL')) + ')
SELECT dat.db, dat.max_intProfessionCode
FROM dat
INNER JOIN (SELECT MAX(max_intProfessionCode) AS max_intProfessionCode_overall
FROM dat) y
ON dat.max_intProfessionCode = y.max_intProfessionCode_overall' AS scrpt
FROM dat;
Make sure the above is only returning data for the appropriate databases by editing the WHERE clause in the CTE, then copy the output, paste elsewhere and run it to get your results.

Why doesn't this GROUP BY query work?

I'm querying my Access table with this query:
SELECT (VIN&' '&Make&' '&Model&' '&CarYear&' '&ExColor&' '&InColor&' ')as CarDescript
FROM TestTable
WHERE (WorkOrderNumber='$workorder')
GROUP BY AssignedEmp;
But a similar type of query works just fine in this SQL Fiddle
Even if I replace the long (VIN&' '&....) with VIN it still doesn't work.
EDIT: Schema of the table is
WorkOrderNumber - Priority - JobStage - WorkItem - AssignedEmp - DueDate - VIN - Make - ... - InColor
In general use + instead of & for SQL. (Access will allow this however).
In a group by you need to pick which one in the group to use (if you are using mysql like your example it just picks a random one, see this fiddle) so to fix this in the general case for your example:
SELECT (max(VIN) + ' ' + max(Make) + ' ' + max(Model) + ' ' + max(CarYear) + ' ' + max(ExColor) + ' ' + max(InColor) + ' ')
as CarDescript
FROM TestTable
WHERE WorkOrderNumber='$workorder'
GROUP BY AssignedEmp;

Query on filters selected by the user

I am making a job portal where users can search jobs by keywords, job category, location salary range, company name.....and I want that if a user select only one option and start searching then he will get all results that belong to that particular option....and if a user select multiple options then result should be filtered according to that...so here i am unable to query data....I just want that the search should be made only by those parameters which are provided by user...other parameters whose values are not provided should not be a part of searching/ query....
This is a bad sample of what i am thinking to do....This query is just for providing you an idea... This is not correct
SELECT employer.phone, employer.mobile, employer.company, employer.country, employer.state, employer.city, employer.address, jobs.title, jobs.category, jobs.salary,
jobs.skills, jobs.contactPerson, jobs.exper, jobs.email AS Expr1, jobs.jobsummary, jobs.posteddate
FROM employer INNER JOIN
jobs ON employer.id = jobs.empid
WHERE (jobs.skills LIKE '%' + #keyword + '%') OR
(employer.city LIKE '%' + #location + '%') OR
(employer.state LIKE '%' + #location + '%')
Use AND logic in the where clause and set the unused variables to a blank and everything will work: if #keywords is blank, (jobs.skills LIKE '%%') will match all jobs.skills.
You can use this approach
SELECT employer.phone, employer.mobile, employer.company, employer.country, employer.state, employer.city, employer.address, jobs.title, jobs.category, jobs.salary,
jobs.skills, jobs.contactPerson, jobs.exper, jobs.email AS Expr1, jobs.jobsummary, jobs.posteddate
FROM employer INNER JOIN
jobs ON employer.id = jobs.empid
WHERE (jobs.skills LIKE '%' + #keyword + '%' OR #keyword is null)
AND (employer.city LIKE '%' + #location + '%' OR #location is null)
AND (employer.state LIKE '%' + #location + '%' OR #location is null)
But remember that it can lead to bad execution plan and it can be better to create separate procedures for different variants of search.

Like clause with Rails 3 and Sqlite

I have a users table with name and surname and a search form with only a full_name field. How can I get it working?
I tried with this simple code:
where_clause = "(name + ' ' + surname) LIKE ?"
# page is a will_paginate method
#users = User.where([where_clause, "%#{params[:full_name]}%"]).page params[:page]
The above code produces:
SELECT "users".* FROM "users" WHERE ((name + ' ' + surname) LIKE '%test%') LIMIT 30 OFFSET 0
... which gives no results! Why?
I think the problem relies in the concatenation, but I can't see any alternatives. In fact I can't modify the select clause (i.e. "*, (name + ' ' + surname) AS full_name") because the page method doesn't work with active record find, and will_paginate methods (i.e. page or paginate) seems not to support conditions any more.
Try using:
where_clause = "(name || ' ' || surname) LIKE ?"

SQL query to linked index server does not work w wildcards

this statement I use in a sproc (where the search phrase 'reiseportal*' is a parameter) does not work with a wildcard, as I recognized:
DECLARE #strSQL NVARCHAR(MAX)
SELECT #strSQL= 'SELECT FileName, path, size, vpath from "GRIP-SERVER"."Web2"..SCOPE() where contains
('SELECT #strSQL=#strSQL + CHAR(39) + CHAR(39)+ 'reiseportal*' + CHAR(39) + CHAR(39)+')'
SELECT #strSQL='SELECT DISTINCT DOC.ID_Kandidat, IDXS.* FROM
OPENQUERY([GRIP-SERVER],'+ CHAR(39) + #strSQL + CHAR(39) +') AS IDXS INNER JOIN
tblK_Dokumente AS DOC
ON DOC.Link = IDXS.[FileName]
ORDER BY ID_Kandidat'
EXEC sp_executesql #statement = #strSQL
these are the contents of the #strSQL variable:
after 1st select:
select FileName, path, size, vpath from "GRIP-SERVER"."Web2"..SCOPE() where
contains(''reiseportal*'')
2nd select:
SELECT DISTINCT DOC.ID_Kandidat, IDXS.* FROM
OPENQUERY([GRIP-SERVER],'select FileName, path, size, vpath from "GRIP-SERVER"."Web2"..SCOPE() where
contains(''reiseportal*'')') AS IDXS INNER JOIN
tblK_Dokumente AS DOC
ON DOC.Link = IDXS.[FileName]
ORDER BY ID_Kandidat'
GRIP-SERVER is the linked Index Server (=Microsoft Server 2003 - our file server).
I don't get it ... it returns results with "reiseportals" but not "reiseportal*" or "reiseportal%". Do you have any hints for me?
Your help is greatly appreciated, thanks a lot!
I found the answer myself.
The problem is not the wildcard, but the string-markers. The index server does not use apostrophes but quotation marks. So the correct SQL should be like:
SELECT DISTINCT DOC.ID_Kandidat, IDXS.* FROM
OPENQUERY([GRIP-SERVER],'select FileName, path, size, vpath from "GRIP-SERVER"."Web2"..SCOPE() where
contains(''"reiseportal*"'')') AS IDXS INNER JOIN
tblK_Dokumente AS DOC
ON DOC.Link = IDXS.[FileName]
ORDER BY ID_Kandidat