I am trying to set a search filter to my view. Normally the view returns the full data table. Now i have 3 filter fields for this first name, last name, and country. I want if the user presses search while all are empty it will return all the data in the db. but if even on field has a string it will return all the values matching that string of that field. similarly if other fields are also full the returned data will be the rows matching the corresponding strings in the fields.
Since i have used pagination using stored procedure i would prefer to use stored procedures for this as well. But i am not as well versed in mysql so i don't really know how to structure my query so that i can check all the conditions in the query and return the result from there instead of getting the data first and then filtering it.
I need the db to return nothing if even one input doesn't matches its corresponding value. but so far i am getting all the row if all the fields have a value and even one field is wrong, and i get the rows even if they do not match with the previous field for example:
if i search a john in first name and enter kelly in the last naem i will get all the john's and kelly's whether the first name doesnt match.
My stored Procedure:
CREATE PROCEDURE [dbo].[GetFilterResult]
#PageIndex INT,
#pageSize INT,
#attr1 nvarchar(300),
#attr2 nvarchar(300),
#attr3 nvarchar(300)
AS
Begin
SELECT Accounts.firstName, Accounts.lastName, Accounts.Email,
Accounts.dateOfBirth, Accounts.phoneNo, Countries.CountryName
FROM Accounts INNER JOIN Countries On Accounts.CountryID =
Countries.CountryID
Where (Accounts.firstName LIKE CONCAT('%', #attr1, '%') and
Accounts.lastName LIKE CONCAT('%', #attr2, '%') and Accounts.CountryID
Like CONCAT('%', #attr3, '%'))
or (Accounts.firstName LIKE CONCAT('%', #attr1, '%') and
Accounts.lastName LIKE CONCAT('%', #attr2, '%') and #attr3 like CONCAT
('null'))
or (Accounts.firstName LIKE CONCAT('%', #attr1, '%') and
Accounts.CountryID LIKE CONCAT('%', #attr3, '%') and #attr2 like CONCAT
('null'))
or (Accounts.CountryID LIKE CONCAT('%', #attr3, '%') and
Accounts.lastName LIKE CONCAT('%', #attr2, '%')and #attr1 like CONCAT
('null'))
or (Accounts.firstName LIKE CONCAT('%', #attr1, '%')and #attr3 like
CONCAT ('null') and #attr2 like CONCAT ('null'))
or (Accounts.lastName LIKE CONCAT('%', #attr2, '%')and #attr3 like
CONCAT ('null') and #attr1 like CONCAT ('null'))
or (Accounts.CountryID LIKE CONCAT('%', #attr3, '%')and #attr1 like
CONCAT ('null') and #attr2 like CONCAT ('null'))
ORDER BY UserId OFFSET #PageSize*(#PageIndex-1) ROWS FETCH NEXT
#PageSize ROWS ONLY;
END
Related
I'm trying to select all values in a table based off the userinput. My issue is when I try to write the query and include the '%' character, I always run into errors. I've tried at least 10 different placements, but I always receive an error. The database being used is MySQL.
SELECT * FROM clients WHERE UserName = %:UserName;
If you want to use the operator LIKE with the wildcard '%' you must concatenate it to the user's input:
SELECT *
FROM clients
WHERE UserName LIKE CONCAT('%', :UserName);
If you want all rows with UserName that contain :UserName:
WHERE UserName LIKE CONCAT('%', :UserName, '%');
I am building a query in MySQL to get records where soundex of a particular column is like soundex of a given value
I am submitting this query:
select nombre_busqueda,
soundex(nombre_busqueda),
soundex('derticia'),
concat('%', (soundex(nombre_busqueda)) , '%')
from jugador j
WHERE idjugador=472
And getting the following
nombre_busqueda
dertycia oscar alberto alvarez
soundex(nombre_busqueda)
D632641634162
soundex('derticia')
D632
concat('%', (soundex(nombre_busqueda)) , '%')
%D632641634162%
So it looks like perfect to make the comparison and get the results.
But when I issue the following query:
select nombre_busqueda,
soundex(nombre_busqueda),
soundex('derticia'),
concat('%', (soundex(nombre_busqueda)) , '%')
from jugador j WHERE
soundex('derticia') like concat('%', (soundex(nombre_busqueda)) , '%')
Nothing gets returned!
What I am doing wrong? It looks like pretty obvious to me that should work!
Interchange the parameters on either side of the LIKE:
select nombre_busqueda,
soundex(nombre_busqueda),
soundex('derticia'),
concat('%', (soundex(nombre_busqueda)) , '%')
from jugador j
WHERE soundex(nombre_busqueda) like concat('%', soundex('derticia') , '%')
I have created a subquery that searches for a particular string from one table, using the SQL LIKE condition. I would like to use this subquery's result as the string to search for in my main SQL query also using the LIKE condition. I tried the below code but I get syntax errors, although it seems to be the way it should be done...sadly I am not an SQL expert and just trying to feel this out.
SELECT * FROM `allcesseries`
WHERE series_id LIKE '%'+(SELECT industry_code FROM `ceindustry` WHERE industry_name LIKE '%Technical and trade schools%')+'%'
SELECT * FROM `allcesseries`
WHERE series_id LIKE concat('%',
(SELECT industry_code FROM `ceindustry`
WHERE industry_name LIKE '%Technical and trade schools%'),
'%')
I would suggest that you use exists in this case:
SELECT *
FROM `allcesseries` a
WHERE EXISTS (SELECT 1
FROM `ceindustry` c
WHERE c.industry_name LIKE '%Technical and trade schools%' AND
a.series_id LIKE CONCAT('%', c.industry_code, '%')
);
If you have multiple matches, then this will work as expected.
You can also phrase this directly as a join, if you want:
SELECT a.*
FROM `allcesseries` a JOIN
ceindustry c
ON c.industry_name LIKE '%Technical and trade schools%' AND
a.series_id LIKE CONCAT('%', c.industry_code, '%')
But if there are multiple rows that satisfy the conditions in ceindustry, you will get duplicates.
I want to find all rows form one table where one field is contained into another field.
For example:
It seems simple:
SELECT * FROM MyTable WHERE name LIKE CONCAT('%', parent_names, '%')
I need 1-st and 3-rd row from this query, but the above doesn't work!
Use INSTR()
SELECT * FROM MyTable
WHERE instr(parent_names, name) > 0
interchange the columns,
WHERE parent_names LIKE CONCAT('%', name, '%')
I'm looking for a way to select something in one column based on another column's data. I have 2 tables:
input_table - has a column called "year_of_birth"
result_table - has a column called "notes"
you see, that "notes" column might contain a year in there, something like "1980". I need to be able to select rows where the notes field contains a match to the year_of_birth field. I have my statement written like this so far:
select inp.year_of_birth, res.notes, res.result
from order_input inp join order_result res on inp.order_id = res.order_id
where res.result !='no match'
and res.notes like '%' + inp.year_of_birth + '%'
right now i'm getting an error thta says "conversion failed when converting the varchar value '%' to data type int. I'm not sure what i'm doing wrong because I have a similar statement i'm using that has this type of string in it that works...
You need to use CONCAT instead of +
res.notes like CONCAT('%', inp.year_of_birth, '%')
Try this:
select inp.year_of_birth, res.notes, res.result from order_input as inp join order_resultas res on inp.order_id = res.order_id where res.result <> 'no match' and res.notes like CONCAT('%', inp.year_of_birth, '%')