find if one column contains another column - mysql

anyone know to how to create a query to find out if the data in one column contains (like function) of another column?
For example
ID||First_Name || Last_Name
------------------------
1 ||Matt || Doe
------------------------
2 ||Smith || John Doe
------------------------
3 ||John || John Smith
find all rows where Last_name contains First_name. The answer is ID 3
thanks in advance

Here's one way to do it:
Select *
from TABLE
where instr(first_name, last_name) >= 1;

Try this:
select * from TABLE where last_name LIKE '%' + first_name + '%'

WHERE Last_Name LIKE '%'+First_Name+'%'
You could also use INSTR(), but take note, both methods perform full-table scans, a general no-no when dealing with high-performance MySQL.

Related

How to use LIKE operator in MYSQL?

Write an SQL query to report the patient_id, patient_name all conditions of patients who have Type I Diabetes. Type I Diabetes always starts with DIAB1 prefix.
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| patient_id | int |
| patient_name | varchar |
| conditions | varchar |
+--------------+---------+
This table contains information of the patients in the hospital.
patient_id is the primary key for this table. conditions contains 0 or more code separated by spaces.
So this was my solution:
SELECT *
FROM Patients
WHERE conditions LIKE 'DIAB1%' OR conditions LIKE '%DIAB1%' ;
It worked correctly for all these conditions
patient_id
patient_name
conditions
1
Daniel
YFEV COUGH
2
Alice
3
Bob
DIAB100 MYOP
4
George
ACNE DIAB100
except for this condition
patient_id
patient_name
conditions
1
Daniel
SADIAB100
And in the solution it was shown that there is a space after 1st % which would give you the correct answer:
correct query:
SELECT *
FROM Patients
WHERE conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%' ;
So, can someone please explain why this query works for that particular condition (SADIAB100) and not the 1st query
WHERE conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%'
The problem this is trying to address is when a condition contains the keyword (DIAB1) - while you only want to match on the beginning of the keyword.
The naive approach fails, because it matches on "SADIAB100":
WHERE conditions LIKE '%DIAB1%'
So the workaround is to search for the keyword:
either at the beginning of the whole string (ie at the beginning of the first condition) ; that's what LIKE 'DIAB1%' does
or after another condition, in which case it is preceded by a space, so ' DIAB1%'
Hence:
WHERE conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%'
A slightly neater expression is:
WHERE CONCAT(' ', conditions) LIKE '% DIAB1%'
Bottom line: if you are using a relational database, you should not be storing multiple values in a single row.
Instead of a CSV-like format, you should have a separate table to store the conditions, with each value on a separate row, allowing you to leverage the powerful set-based features that your product offers.
creating a regex pattern to find the keyword 'DIAB1'
filtering on cases where it matches the above
with main as (
select
*, REGEXP_LIKE(conditions,'DIAB1') as is_relevant_patient
from <table_name>
)
select * from main where is_relevant_patient
fiddle test

Count values of a Multi-Valued attribute for each Row in MySql

I have the following schema:
Contact:
ID| NAME |Contact
----------------------
1 | A |1234,4567
-----------------------
2 | B |2345,5678,9012
Here, as you can see Contact is a Multivalued field and A person can have as many as contact numbers.
Now, I want to count the number of contacts for each person.
How can I do that?
Note: I don't want to normalize this table.
Any help will be appreciable.
While I don't advise doing this, this is how you would do it:
SELECT ID, (LENGTH(Contact) - LENGTH(REPLACE(Contact,",","")) + 1) AS NumberOfContacts
FROM MyTable
The output will look like this:
ID| NumberOfContacts
1 | 2
2 | 3
The difference in length after the replace will tell you how many commas were in the string, then you would need to add 1 to include the last number. You should really structure the data properly, but if that is out of the question then you will need to work with something like this

MYSQL Wildcard issue for my query

ive got an issue with the mysql wildcard.
Given is the following table:
id | name
--------------
1 | Marcel
2 | Marcel2
3 | Marcel3
Now i need to know how many entrys with the name Marcel are in the table.
SELECT id FROM 'users' WHERE name LIKE 'Marcel#'
--gives me **0** results
SELECT id FROM 'users' WHERE name LIKE 'Marcel_'
-- gives me just **1** result (**3** would be the solution)
What query could i use to identify how often the name + nr is in the table?
Use % wildcard:
SELECT id FROM `users` WHERE name LIKE 'Marcel%'
SqlFiddleDemo
% A substitute for zero or more characters
_ A substitute for a single character
So your:
SELECT id FROM 'users' WHERE name LIKE 'Marcel_' gives me just 1
result (3 would be the solution)
may be false see: SqlFiddleDemo because it gives you Marcel2 and Marcel3 (unless you have trailing whitespaces which I cannot see in example you've provided)
Name like '%Marcel%' Should work for you.

Advance search on database column

I have a Table say tblName having fields and values
| ID | Name |
| 1 | Technical University |
| 2 | XYZ Lab Ltd. |
Now I have to match Name column with the following input values :
1- tech univ
2- tech universities
3- xyz Labs
I am not able to write query to get result from the table.
If I match Name column with the input value tech univ or tech universities then query should response result set Technical University from the tblName.
Please suggest.
Thanks in advanced.
Well you can do one thing map name to different names such as
ID I_ID NAME
1 1 tech university
2 1 tech uni
3 1 tech univs
So this table will be link for your search queries I_ID is a foreign key which refers ID in primary table. I guess this could solve your issue and you can define several names for a single name.
Moreover use regular expression for more efficiency and you have to develop a custom function for that though the genral usage of REGEXP is as follows
SELECT id, name FROM tblName WHERE name REGEXP 'tech'
Please refer http://www.tutorialspoint.com/mysql/mysql-regexps.htm for more on REGEXP
For simple cases when you need to match begining of string you can use
select * from table where field LIKE 'Hello %'
The % sign is a wildcard.
But for the example you need I would rekomend to look on some full text indexing solutions that you put around mysql.
In MS SQL we use a Levenstein Distance function and you could build your own and extend mysql with it.
Here is some examples but I do not know if it will work
https://github.com/jmcejuela/Levenshtein-MySQL-UDF
https://github.com/jmcejuela/Levenshtein-MySQL-UDF/blob/master/levenshtein.c
Found one that should be pure SQL
http://openquery.com.au/blog/levenshtein-mysql-stored-function
test this it may help
SELECT * FROM tblNAME WHERE Name LIKE "%name%";

What's the point of ordering by 2+ columns in SQL?

I've been experimenting with this particular table:
http://www.quackit.com/sql/tutorial/sql_order_by.cfm
and it seems when I order by more than 2 columns, I get the same results as ordering by one column.
For example:
SELECT * FROM Individual ORDER BY last_name;
is basically the same as saying:
SELECT * FROM Individual ORDER BY last_name, first_name;
What's the whole point of ordering by multiple columns in SQL? I really see no practical use of it, are there some things you can accomplish with it that you can't accomplish in sorting by same column?
It is not the same.
While ORDER BY last_name may produce a result like
last_name | first_name
Doe | John
Doe | Jane
ORDER BY last_name,first_name is always
last_name | first_name
Doe | Jane
Doe | John
If 2+ people have the same last name, the second sort column will sort by their first name.
it could be that there is a index on first_name and last_name column and they are being sorted on the index.