how to search between three different values in mysql? - mysql

I have a MySql database with some data like this:
str = '554a41a6e4d9d 677 3'
and i want to search all the values that exactly match the '677' part (the second part of text, after the first).
How i can accomplish this?

If the 'parts' are delimited by space, you can use like:
SELECT [cols] FROM [table] WHERE [col] LIKE '% 677 %'

You should use Fulltext type for the column. And then use a query like this:
SELECT * FROM table WHERE MATCH (column) AGAINST ('677');

Related

WHERE column contains single word

How can I use SQL like to only display single words that are in a particular column of any row? For example, suppose I have the following rows in column1.
Column1
put return
need return
got return
bring return
server
client
The single-words output I would like is:
server
client
Can this be accomplish using like? I know this can be accomplished using regular expressions, but if possible, I would prefer using LIKE.
You can use not like:
where col not like '% %'
SELECT * FROM YourTable
WHERE Column1 NOT LIKE '% %'
Or:
WHERE LOCATE(' ',Column1) = 0

Query to select data from mysql with specific condition

In MySQL we can use this code to select rows with ID numbers (or anything else) between a list:
SELECT * FROM TABLENAME WHERE prophrases IN (1,2,3,4,5,6)
Thats OK! but if we need to search a number in a Field's value, what we can do?
For example, I have a table with a field, named 'prophases' and I saved data like this:
rowid / prophases
1 / 1,2,3,4,5,6,7,8
2 / 6,5,2,7,9,2
now, i need to check if a number like 6 is in prophrases in row #1 or not!
what can i do for that?
something like this but in correct form!
SELECT * FROM TABLENAME WHERE 6 IN prophrases
you should just use FIND_IN_SET()
SELECT rowid
FROM TABLENAME
WHERE FIND_IN_SET('6', prophrases)
This will work if you are only dealing with single digits
select * from tablename where prophrases like '%6%'
This will work if you are going to have number with more than one digit and there are no spaces between commas.
select * from tablename where prophrases like '%,6,%' or prophrases like '6,%' or prophrases like '%,6'
You need to use a like statement
SELECT * FROM TABLENAME WHERE prophrases LIKE '%6%'
However if you have multiple digit numbers that could cause some unexpected results, so you might have to amend it like
SELECT * FROM TABLENAME
WHERE prophrases LIKE '%,6,%'
OR prophrases LIKE '6,%'
OR prophrases LIKE '%,6'
The first part matches 6 in between commas, the second one matches a 6 at the beginning followed by a comma, the third matches a comma followed by a six a the end.
Storing data like that is not the best way of doing it. You are probably better off having another table that has a one-to-many relationship.

A query to check the first 2 chars of a string

I would like to know if it's possible to make a query to check the first two chars of a string?
For example, I have these entries in my table:
toto
topo
poto
I want to get the entries when the two first chars are to.
As simple as this:
SELECT *
FROM TABLE
WHERE `name` LIKE "to%"
to% means that the word should start by to and can be followed by other character
Try this:
SELECT * FROM myTable WHERE RowName LIKE 'to%'
select fieldName
from tableName
where fieldName like 'to%'
if you need to check the first 2 chars of a string but return all results..
SELECT fieldname,
if(left(fieldname,2)='XX',1,0) as XCheck
FROM tablename

How to find certain Hex values and Char() Values in a MySQL SELECT

Anyone know the syntax to search a MySQL table column by hex value or CHAR() value? Do I have to use a LIKE or IN()?
Something like:
SELECT * FROM `myWordTable` WHERE `definition` LIKE 0xE28098;
OR
SELECT * FROM `myWordTable` WHERE `definition` LIKE CHAR(128);
Thanks!
The second one is close.
Try this:
SELECT *
FROM `myWordTable`
WHERE `definition`
LIKE concat('%',CHAR(128),'%');
In the first example consider pattern match '%' == 0x25
SELECT *
FROM `myWordTable`
WHERE `definition`
LIKE 0x25E2809825;
You need to UNHEX() values you want to match on.
For example, finding all accented e's that were corrupted into 0xC3A9:
SELECT id, FirstName
FROM Person
WHERE FirstName
RLIKE UNHEX('c3a9');

"Merge" IN and LIKE operator in Mysql

I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.