I have a huge document with names and surnames, it currently looks like this:
"name1 surname1", "name2 surname2", "name3 surname3", "name4 surname4"....
In database in users table there are columns name surname email.
What I need now is to find emails of the people from the list.
I cannot separate names and surnames since there are dozens of them.
What is the best way to find them? I guess %like% won't be working here. Please help me on this.
Note: What is difficult, some names and surnames are made of 3-4 words. This is not always only name and surname as 2 words.
DECLARE #mytxtvar TEXT;
SET #mytxtvar = LOAD_FILE('D:\\test.txt');
SELECT #mytxtvar;
SELECT email FROM user WHERE concat(name,' ',surname ) in #mytxtvar
I don't get your problem.. isn't a simple query like this one enough?
select email
from user
where
(name = 'name1' and username = 'surname1') or
(name = 'name2' and username = 'surname2') or
...
You just need to split your initial list with some language that you like or even a simple text editor and then execute the query
You can split each person on name and surname by using any language like JAVA,
Then make a batch query job for all names using loop and save the result wherever you want.
The query will be like this::
select email from user where name ='name1' and username ='surname1'
Related
I have a column in my SQL database (using mySQL) 'lastname', which has any number of employee's last names in it. I want to write a query that handles a search for last names using a comma delimited list.
So the user will type:
Garcia, Smith, Jones
And that gets stored in a String, lastNameQuery, which then gets passed into my backend API function that calls the SQL command. I want to return all employees in the DB that have those last names.
Is there any kind of SQL SELECT command I can use that search using a list like that? For my other functions (which only handle a single search term) I'm using this:
"SELECT * FROM employees WHERE salary LIKE '%${salary}%'"
Which works great. Is there some way I can modify it to handle a list? I can always break up the single String ("Garcia, Smith, Jones") into an array if necessary so that's not a problem. Any ideas?
You need to either do:
(lastname like '%Garcia%' or lastname like '%Smith%' or lastname like '%jones%')
or create a fulltext index on lastname (alter table employess add fulltext (lastname)) which would let you do
match (lastname) against ('Garcia, Smith, Jones')
but won't do things like find Garcia if you search for just "Garc".
Am working on a scenario, where I want to search for a string value from URL is matching 2 columns from the database. For example.. in my project system, there is customer first_name and last_name is there while entering web URL like website.com/nayanachandran
the word "nayanachandran" has to check from the customer database, with laravel queries like the combination of customer first_name and last_name is equal to the string in request param.
currently, I did it in a way like added hi-pen in string
eg: nayana-chandran
and while searching I added query in the repository like:
$publicUrl = explode("-",$this->request->unique_name);
$coach_details = Customer::where('first_name', $publicUrl['0'])->where('last_name', $publicUrl['1'])->first();
Any help ?
It sounds like you're asking how you can take a concatenated string from the url, nayanachandran, and search for that in your DB as a combination of the columns first_name and last_name.
Assuming that's the case, you can use a combination of whereRaw and CONCAT:
Customer::whereRaw("CONCAT(first_name, last_name) = ?", [$this->request->unique_name])->first();
Alright, so a way to do this would be to:
$publicUrl = explode('-', $this->request->unique_name);
list($firstName, $lastName) = $publicUrl;
Which indeed creates an array from the string delimited by '-', and then simply do a:
Customer::where([['first_name', $firstName],['last_name', $lastName]])->first();
Which will return you the Customer object related to that condition.
However, let me recommend you to use query strings instead, and simply have an address like:
https://address.com/example?first_name=james&last_name=smith
Which would render the explode unnecessary since you could access those values directly from the request object:
Customer::where([['first_name', request()->get('first_name')],['last_name', request()->get('last_name')]])->first();
I need to clear out invalid users created by a scipted attack from the database.
The query would:
check if row name "about:me" contains "michael kors"
if yes, then set row "account_status" to "inactive"
I'm a very novice "DBA" so I'm hoping an example of a correct query in this case will help me to be able to construct others and help other users do the same.
If the column name is really about:me (a colon would be unusual), and account_status is actually a text field (not an integer indicating active/inactive), and imagining your table is named users (you didn't say), something like this would work:
update users
set account_status = 'inactive'
where `about:me` like '%michael kors%';
The backticks allow unusual characters like : to appear in the column name; the % allows varying whitespace (or other random characters) around the name when used with like; the like also allows us to match regardless of upper/lowercase in the name column.
assuming you're describing a table 'tbl' with fields 'about:me' and 'account_status', you should be able to run an UPDATE with a WHERE to do this. Something like...
UPDATE tbl
SET account_status = 'inactive'
WHERE `about:me` LIKE '%michael kors%';
Note this uses LIKE which means it's case insensitive and uses the wildcard character % to match anything containing michael kors.
Hope it helps!
I have a database that the user needs to search using wildcards. For example, the column Order number the user would need search by tickets starting with 3011* or 201*. Since the ticket number has many different numbers it would need to be build by parameter. I know I have to use LIKE and I know how to write the query with LIKE if the search would start with the same number all the time, but I do not know how to write it using a parameter for user input.
For example:
ITEM ORDER_NO
Chair Order 3011134
Table Order A230445
Tile Order 1032234
So when the user inputs a search for orders starting with *3011 the Chair order would pop up.
I know how to search for exact match but not for wildcards
I have done some research on this, but nothing on using a parameter. Any help or guidance will be appreciated.
Have a look at the following : SQL Wildcards
Usually in SQL you will do things like :
declare #Search varchar(20);
set #Search = '3011';
select *
from TableName
where FieldName like '%' + #Search + '%';
This allows you to search for things staring with, ending with and having the search text in the middle of the field you are looking at.
Just be careful for SQL Injections.(Alwasy validate user inputs.)
SELECT item, order_no
FROM 'your table'
WHERE order_no
LIKE '3011%'
The % wildcard symbol with LIKE will return all rows where the order_no starts with 3011 - so from your table the above query would return Chair Order - 3011134
i'm building a search form for a website, with a single input fields (name and lastname) and i need it to match against two database fields (NAME_FIELD and LNAME_FIELD).
Obviously users can type in the name first or the lastname (or only the lastname) as they prefer.
Thank you
Assuming the user inputs are NULL when not used:
select *
from YourTable
where NAME_FIELD = coalesce(#name, NAME_FIELD)
and LNAME_FIELD = coalesce(#lastname, LNAME_FIELD)
Assuming you are using prepared statements:
WHERE (:name IS NULL OR NAME_FIELD LIKE :name)
OR (:lname IS NULL OR LNAME_FIELD LIKE :lname)
Have a look at MyISAM Full Text Search, which will lead to much better performance then the LIKE operator.
The syntax may be off a bit, but you could do something like:
WHERE 'name_field' LIKE %'YOUR_INPUT'% OR 'lname_field' LIKE %'YOUR_INPUT'%
You can use = 'YOUR_FIELD' to match it exactly.