I have value in my db
like
1,12,13,25,44,414,2114
I have to find exact 14 from the db.
but it also return the value 414 and 2114
but i want exact 14.
How can i achieve this through sql query Please help
i have tried this but didn't worked.
Select * from tb_name where columnNAme like '%value%'
If you want search exactly 14, then you need search %,14,%, but 14 may appears at start or at end of string, so you need add commas to column also.
Well, you can use:
select * from tb_name where concat(',',columnNAme ,',') like '%,14,%'
Side note, comma separated values in one column is bad database design
Select * from tb_name where columnNAme like 'value'
% - The percent sign represents zero, one, or multiple characters
SELET * FROM database WHERE column LIKE 14 should work fine, just double checked it on my database.
If this doesn't work can you show your query please?
Above answer is correct, I didn't see your query before I posted this.
Related
I have MySQL table wp422_posts and I need to manipulate one type of values in post_name column. Actually, I need to manipulate long value beginning with 4 digits (unique ID) and than text, I need to get the 4 digits and left them there. Could anybody help me, please? I'm new to MySQL. Other values - which do not start with 4 digits and dash has to stay same.
one example of value of post_name
old:
"2147-sprava-uzivatelskych-uctu-databaze-oracle-v-prostredi-autocad-map-3d"
desired
new one: "2147"
How to select correct values in column:
SELECT * FROM `wp422_posts` WHERE `post_name` LIKE "[0-9][0-9][0-9][0-9]%"
it returns empty result, but why?
How to get rid of not needed part?
Thanks relly much for your help.
If you are sure that starting part is always 4 digit number and that is only needed, then you can use this:-
SELECT LEFT(COL_NAME, 4)
FROM `wp422_posts`
Then you can use update statement to update the table. Somthing like:-
UPDATE `wp422_posts`
SET COL_NAME = LEFT(COL_NAME, 4)
Hope this helps!!
My table contains some columns with ;-separated numbers like this :
1;2;43;22;20;12
and so on. It's also possible there's only 1 number in this column like 110, or 2 numbers like this 110;143
I want to select the rows that contain a certain number in this column. The number is in a variable $search_var.
Let's say I need to search for the number 1 in my column. If I use a select with like statement like so :
"SELECT * FROM Table WHERE ids LIKE '%".$search_var."%'"
I get all results containing '1' and not only '1', so I also get 11, 14, 110, 1999 etc.
I think I need to use some sort of regex-statement but I'm lost here... Who can help ?
You might not need regex for this
Set #YourNUmber := 110;
SELECT *
FROM Table
WHERE ';' + ids + ';' LIKE '%;'+ #yourNumber + ';%'
This guarantees there are always ; surrounding all the numbers.
This is formatted for SQL Server. The variable syntax and wildcards might be different if you are using something else.
EDIT:
Thanks #FĂ©lixGagnon-Grenier for the suggestions. I think either of these two will work. See here for a SQL Fiddle example
SELECT *
FROM T
WHERE concat(';',ids,';') LIKE concat('%;', #YourNumber , ';%');
SELECT *
FROM T
WHERE LOCATE(concat(';', #YourNumber , ';'),concat(';',ids,';'))>0
Try this solution if you're using SQL Server. This searches for the number where adjcent characters are not numbers:
SELECT * FROM Table WHERE ids LIKE '%[^0-9]".$search_var."[^0-9]%'
Ok, so here is the issue.
I have a table with some columns and 'subject' is one of the columns.
I need to get the first 10 letters from the 'subject' field no matter the 'subject' field contains a string with 100 letters.
For example,
Table - tbl.
Columns - id, subject, value.
SQL Query:
SELECT subject FROM tbl WHERE id ='$id';
The result I am getting is, for example
Hello, this is my subject and how are you
I only require the first 10 characters
Hello, thi
I can understand that I can remove the rest of the characters using php substr() but that's not possible in my case. I need to get the excess characters removed by MySQL. How can this be done?
Using the below line
SELECT LEFT(subject , 10) FROM tbl
MySQL Doc.
SELECT SUBSTRING(subject, 1, 10) FROM tbl
Have a look at either Left or Substring if you need to chop it up even more.
Google and the MySQL docs are a good place to start - you'll usually not get such a warm response if you've not even tried to help yourself before asking a question.
I have a MySQL table column rubrics which contains string value '61,80,112,256'. So I try execute that query:
select * from table where 256 in (rubrics) and 61 in (rubrics)
And no result returns. Any suggestions?
Since your rubrics column is a comma separated list the IN operator will not work.
MySQL does have a function that can find a value in a string list so you should be able to use FIND_IN_SET():
select *
from yourtable
where find_in_set(61, rubrics)
or find_in_set(256, rubrics)
See SQL Fiddle with Demo
Something like WHERE rubrics LIKE '%,256,%' OR rubrics LIKE '256,%' OR rubrics LIKE '%,256'. Using parenthesis you can also filter on the 61, but the resulting query will be come messy. You'd better normalize your data, so you can use subqueries and joins using keys (the real deal).
(see also bluefeet's answer as FIND_IN_SET is a better approach)
Try this
select * from table where rubrics like '%'+'256,'+'%' and rubrics like '%'+'61,'+'%'
IN operator does not work with strings
use correct substring matching operator like LIKE or LOCATE
one advice - update your rubics column to begin and end with , character, that will make your LOCATE(",62,", rubics) operations unambiguous as opposed to LOCATE("62", rubics) which will match also 622 and 262 and other combinations. Locating ,62, wil fail if your rubics has value of 62,blah,foo,bar because it doesn't start with ,
I use a string for store the days of the week, something like this:
MTWTFSS. And if I search for MF (Monday and Friday) then the query must return all the strings that contain MF (for example: MWF, MTWTFS, MF, and so on).
I don't know how to do this in SQL (MySQL).
use LIKE with %-wildcard between the single characters:
SELECT * FROM table WHERE column LIKE '%M%F%';
note that this will only work if the characters are in correct order - searching for FM instead of MF won't give you any result.
you'll also need to find a way to insert the %s to your search-term, but taht shouldn't be a big problem (sadly you havn't said wich programming-language you're using).
if the characters can be in random order, you'll have to built a query like:
SELECT * FROM table WHERE
column LIKE '%M%'
AND
column LIKE '%F%'
[more ANDs per character];
SELECT * FROM yourTable WHERE columnName LIKE '%MF%'
Learn more:
http://www.sqllike.com/
Can you not just say
SELECT * FROM blah WHERE weekday LIKE "%MF%"