Column1 has a record with the value example.
A user submits the value somethingwithexample.
The end of his string matches the value in Column1. How to SELECT Column1 in MySQL?
You could use the like operator:
SELECT *
FROM mytable
WHERE :userinput LIKE CONCAT('%', mycolumn)
EDIT:
As noted in the comment, if the column is empty, this will evaluate as userinput LIKE '%', which is by definition always true. This edge-case should be handled explicitly:
SELECT *
FROM mytable
WHERE mycolumn IS NOT NULL AND
mycolumn != '' AND
:userinput LIKE CONCAT('%', mycolumn)
select * from table_name
where column_name like '%example'
select * from your_table
where $userInput like concat('%', col)
select * from tablename where value like '%example';
will select all rows whose value column has values that are ending with example
for example: mathexample, scienceexample;
select * from tablename where value like '%example%';
will select all rows whose value column has values such as mathexample1, scienceexample2 etc
Related
I have a varchar column in a table and I need to find all values in the column that matches a pattern. The pattern is either parta-partb-partc or parta-partb-positiveInteger-partc. Except for the number part, everything is fixed.
For example
**someColumn**
parta-partb-partc
parta-partb-1-partc
parta-partb-1xyz-partc
parta-partb-123-partc
parta-partb-abc-partc
My search query should return
parta-partb-partc
parta-partb-1-partc
parta-partb-123-partc
So far this is what I got
SELECT *
FROM tableName
WHERE
someColumn ='parta-partb-partc'
OR someColumn LIKE 'parta-partb-%[0-9]-partc'
I am not able to construct the LIKE part to get only strings with positive number in between.
For MySQL following query gives expected result
SELECT *
FROM
tableName
WHERE
someColumn = 'parta-partb-partc'
OR
someColumn REGEXP 'parta-partb-[0-9]+-partc'
;
For Oracle following query gives expected result
SELECT *
FROM tableName
WHERE
someColumn = 'parta-partb-partc'
OR
REGEXP_LIKE(someColumn, 'parta-partb-[[:digit:]]+-partc')
;
For MS-SQL unfortunately REGEX is not supported in where clause, so you have to do something like following. [Specific to your example data]
SELECT *
FROM tableName
WHERE
someColumn = 'parta-partb-partc'
OR
( someColumn LIKE 'parta-partb-[0-9]-partc'
OR someColumn LIKE 'parta-partb-[0-9]%[0-9]-partc'
AND someColumn NOT LIKE 'parta-partb-[0-9]%[a-zA-Z]%[0-9]-partc'
AND someColumn NOT LIKE 'parta-partb-[0-9]%[a-zA-Z]-partc'
)
I have 2 columns abc and bcd i want to compare abc data with bcd column exact match and sentence contain abc.
Expected Result
Are you trying to select all rows where there exists a row the abc of which matches the rows' bcd? Then use EXISTS.
select *
from mytable
where exists
(
select null
from mytable other
where mytable.bcd like concat('%', other.abc, '%')
)
order by abc, bcd;
You can achieve it like this:
SELECT * FROM table_name WHERE table_name.abc like CONCAT('%', table_name.bcd, '%');
This will return all records where string from abc column is contained in a string from bcd column.
Of course, replace table_name with your table name.
Can you do a "SELECT * FROM TABLE WHERE attribute has last character which is 'A' or 'B'"
I see, that everyone who answered here suggest LIKE.
However, the following would be faster than LIKE, so I suggest you use it.
SELECT *
FROM t1
WHERE substring(attribute, -1)= 'A'
OR substring(attribute, -1)= 'B';
SQLFiddle
You can try:
SELECT * FROM TABLE WHERE attribute LIKE '%A' OR attribute LIKE '%B'
Try
SELECT * FROM TABLE WHERE attribute LIKE '%A' or attribute LIKE '%B'
Try this query:
SELECT * FROM TABLE1 WHERE attribute LIKE '%A' OR attribute LIKE '%B';
You can also use Mysql's RIGHT() function
SELECT
*
FROM
`table`
WHERE RIGHT(attribute, 1) = 'A'
OR RIGHT(attribute, 1) = 'B'
Small Fiddle Demo
Yes, it's possible. For instance you could:
SELECT * FROM table WHERE attribute LIKE '%[AB]'
Which would select all rows in table with field 'attribute' ending in either 'A' or 'B'
In a perfect world for this type of setup, we would have an integer column that expects only numbers;
But what if you have a varchar column and you want to add a WHERE clause that said something like this:
WHERE <value> is NOT a number
In essence, you are selecting all rows that contain any characters that are NOT ONLY numbers.
This is for MySQL.
try this
SELECT * FROM myTable WHERE concat('',col1 * 1) != col1
demo here
REGEXP or RLIKE are your friends:
SELECT * FROM `MyTable` WHERE `Column` RLIKE '^[^0-9]*$';
UPDv1:
You may use different regexes to detect negative integers:
SELECT
'-2' RLIKE '^[^0-9]*$', -- fails
'-1' RLIKE '-[0-9]'; -- succeeds
For example:
SELECT * FROM `MyTable` WHERE `Column` RLIKE '-[0-9]' OR `Column` RLIKE '^[^0-9]*$';
Tested with this:
SELECT
*
FROM
(
SELECT 'abs 12 x' as `Column`
UNION ALL
SELECT 12
UNION ALL
SELECT -2
UNION ALL
SELECT '-x'
) as `sub`
WHERE
`Column` RLIKE '-[0-9]'
OR
`Column` RLIKE '^[^0-9]*$';
Output:
-2
-x
You should be able to use a regular expression in the where clause.
The following mysql documentation link provides details:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
This would approach somehow your goal:
SELECT * FROM MyTable WHERE NOT MyTable.Field1 REGEXP '^[[:digit:]]*$';
As Field1 is VARCHAR, this will select all rows that Field1 is not wholly numerical.
If you have floating-point value:
SELECT * FROM MyTable WHERE NOT MyTable.Field1 REGEXP '^[[:digit:]\.]*$';
I'd like to select rows from the database where the last character in the mov_id column equals to 1 and 2.
How would the query look like?
SELECT * FROM `myTable` WHERE `mov_id` LIKE '%1' OR `mov_id` LIKE '%2'
the % character is a wildcard which matches anything (like * in many other places)
If mov_id is a numeric value (TINYINT, INT, etc...) then you should use a numeric operator. For instance, use the modulo operator to keep the last digit
SELECT * FROM mytable
WHERE (mov_id MOD 10) IN (1, 2)
If mov_id is a string, you can use LIKE or SUBSTRING(). SUBSTRING() will be slightly faster.
SELECT * FROM mytable
WHERE SUBSTRING(mov_id, -1) IN ('1', '2')
If your table is big or that query is frequently run, you should definitely consider adding a column to your table, in which you would store mov_id's last digit/character, and index that column.
Try this way too:
SELECT field1
FROM table
WHERE RIGHT(field1, 1) = 'x'
it displays the fields that has last a value of x.
SELECT *
FROM Table
WHERE RIGHT(Column_name, 1) IN ('x')
if you want to match two character just replace 1 by 2.
In general:
RIGHT(COLUMN_NAME, NO_OF_CHARACTER_YOU WANT_TO_MATCH_FROM_LAST)
And if you want to match the starting char just use LEFT instead of RIGHT
You could also do something like:
select
your, fields, go, here
from table
where
substring(mov_id, (char_length(move_id) - 1)) = x
SELECT * FROM table WHERE mov_id REGEXP '1$' OR mov_id REGEXP '2$'