I'm trying to use a query to narrow the table down to only the rows in which the field [full name] contains the value in the field [first name].
For instance, if a row has "Blake Johnson" in [full name] and "John" in [first name] - this row will be included.
But if [full name] has "Garry Sways" and [first name] has "Swan" - this row will NOT be included.
I tried to use:
Like "*[first name]*"
In the criteria for [full name].
But it didn't work quite well.
Is there a "Contains" funciton for this case?
Thanks in advance.
Just do this
SELECT * From yourTable WHERE instr(fullname, firstname) > 0
I am not sure what kind of query language you are using but you can use regular expressions to make a more granular version of "like"
For example, in MySql you can do:
SELECT * FROM 'foo' WHERE 'bar' REGEXP "^\$"
or in your case:
SELECT * FROM table WHERE fullname REGEXP (".*" + firstname + ".*");
Related
Which one of these two queries would perform better in MySQL and/or ORACLE?
SELECT *
FROM User
WHERE name LIKE "%searchTerm%"
OR lastname LIKE "%searchTerm%"
OR email LIKE "%searchTerm%";
or
SELECT *
FROM User
WHERE CONCAT(name, " ", lastname, " ", email) LIKE "%searchTerm%";
I have a strong feeling that the second one, but I'd like to be sure.
SELECT *
FROM User
WHERE name LIKE "%searchTerm%"
OR lastname LIKE "%searchTerm%"
OR email LIKE "%searchTerm%";
This query is better then second one. Because if you want to use different filter on all the Fields then you can use them easily.
SELECT *
FROM User
WHERE CONCAT(name, " ", lastname, " ", email) LIKE "%searchTerm%";
In this query you can not use different filter. You have to use common filter on all the fields.
First of all the two queries are not equivalent. In case that a blank (the concatenation delimiter) may be used in the searchTerm, the first query may not match, but the second can, because the match spans two or three columns.
Performance will be very same as both queries make full table scan and the difference is only in the filter condition
First Query
1 - filter("NAME" IS NOT NULL AND "NAME" IS NOT NULL AND "NAME" LIKE
'%searchTerm%' OR "LASTNAME" IS NOT NULL AND "LASTNAME" IS NOT NULL AND
"LASTNAME" LIKE '%searchTerm%' OR "EMAIL" IS NOT NULL AND "EMAIL" IS
NOT NULL AND "EMAIL" LIKE '%searchTerm%')
Second Query
1 - filter("NAME"||' '||"LASTNAME"||' '||"EMAIL" LIKE '%searchTerm%')
So basically neither query is suitable for a customer search on non trival tables.
You typically want to limit the search to column LIKE 'xxxxxx%' which can use an index.
(MySQL Answer)
Either way will involve a full table scan, so neither will be fast. (Fetching rows is more costly than evaluating expressions.) Furthermore, the leading wildcard implies that the string(s) must be fully scanned; no INDEX usable possible.
If your "serachTerm" is always word(s), then use FULLTEXT(name, lastname, email) and MATCH(name, lastname, email) AGAINST ("+searchTerm" IN BOOLEAN MODE); it will be a lot faster.
i need to find multi string in multi columns in mysql.
select * from myTable WHERE name LIKE '%stack%';
This obviously works. However, what I need to do is that if both name and surname are checked multi string would do something like this:
select * from myTable WHERE (name or surname) LIKE '%stack%','%over%','%flow%';
if similar above code that work does not exist. please tell me how can checking multi string in a column in mySQL with like statement:
select * from myTable WHERE name LIKE '%stack%','%over%','%flow%';
You can keep it simple and do:
select * from myTable WHERE name LIKE '%stack%' OR
name LIKE '%over%' OR
name LIKE '%flow%';
or:
select * from myTable WHERE name LIKE '%stack%' AND
name LIKE '%over%' AND
name LIKE '%flow%';
This doesn't look as nice as the REGEXP from Young, but it doesn't have the unexpected behaviour REGEXP can have given that user input can be anything. With REGEXP you really need to know what you're doing.
You can try:
select * from myTable WHERE name REGEXP 'stack|over|flow';
Though it's not a like solution.
I'm very new to access. I have a data in my column that looks similar to this:
JONES/KEN
SMITH/TAMMY
MILLER FRED
PICARD.JOHN
Am I able to grab the letters before the first non-alphanumeric?
So my result would be:
JONES
SMITH
MILER
PICARD
How about a derived table:
SELECT Left([Surname],InStr([Surname],[NonAlpha])-1) AS LeftName,
MainTable.Surname
FROM MainTable,
(SELECT " " As NonAlpha From Table1
UNION
SELECT "." As NonAlpha From Table1
UNION
SELECT "," As NonAlpha From Table1
UNION
SELECT "/" As NonAlpha From Table1) AS n
WHERE (((MainTable.Surname) Like "*" & [nonalpha] & "*"));
Table1 is a scratch table, it does contain records but the query will only
return the four assigned rows (,./ )
Maintable is the table with a field Surname, which is the field to be split.
Unfortunately, I don't know of a "Word" function that's available in some languages. I would do it with brute force checking using Instr and then Mid to extract the code. The construct would be very convoluted to get every kind of character.
I've used the iif function and nested it - this the basic format here:
iif (instr (fieldname,"{the character}") > 0,
mid(fieldname,1, instr(fieldname,"{the character}")-1,
fieldname{or go further into ifs})
Using your sample data with Client Name as the field and 3 conditions - space, / and period. IT does work, but it's ugly - you will have to scroll pretty far to the right to get everything:
ShortName: IIf(InStr(1,[client_name]," ")>0,
mid(client_name,1,InStr(1,[client_name]," ")-1),
IIf(InStr(1,[client_name],"/")>0,
mid(client_name,1,InStr(1,[client_name],"/")-1),
IIf(InStr(1,[client_name],".")>0,
mid(client_name,1,InStr(1,client_name],".")-1),
Client_Name)))
Put this in a query based on your table.
I have in database TEST these values for example:
ID
1_0
1_1
10_1
11_1
If I want select all values at the beginning with number "1", but not "10" or "11" then I have created my request in that way:
SELECT * FROM test WHERE id LIKE '1_%';
But sign _ for Mysql Query also means one character from name so result of this will be all the values from the list.
My question is - how to force query to think that _ is a character in ID name, not sign of any letter?
I think you should be able to just escape the _ like so:
SELECT * FROM test WHERE id LIKE '1\_%';
How can I find all the ids of an SQL table? For example I want to search for the word "key" and return the ids in which this word was found.
Assuming that id is the name of a column in your table you would need to use LIKE
SELECT id
FROM YourTable
WHERE id LIKE '%key%'
The % is a wildcard meaning match any set of zero or more characters so this would find rows where the id value contains the substring (not necessarily word) "key".
SELECT [id field name] FROM [Tabel Name] WHERE [field to seach in for key] LIKE '%key%';
Would this be what you are looking for?
INSERT INTO dest_table (dest_id)
SELECT source_id
FROM source_table
WHERE source_column LIKE '%key%';