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!!
Related
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.
I have a quite big table in mysql and I need to change all the records related to this column.
records are like this :
/name/nm0000209/?ref_=ttfc_fc_cl_t1,
/name/nm0000151/?ref_=ttfc_fc_cl_t2,
...,
/name/nm0104594/?ref_=ttfc_fc_cl_t10
what I want is to keep only the string in the middle which is nm0000209, nm0000151,.... I know how to delete specific characters from the right or left of the words by REPLACE or Trim , .., but my problem is that in this case the number of characters in the third part of string are not equal (as you see when it reaches to 10, I have to delete 21 characters from the end instead of 20 characters and since this table contains lots of records I dont know how to do it.
I reaaly appreciate if someone could helop me,
thanks
I want is to keep only the string in the middle which is nm0000209, nm0000151...
You can use 'SUBSTRING_INDEX' on the column to crop part of the column value.
Following example assumes that the said column will have 'name/' as starting pattern.
Example:
update table_name
set column_name = substring_index(
substring_index( column_name, 'name/', -1 )
, '/', 1 );
The same can be used for updating with the same value.
Demo # MySQL Fiddle
One approach would be to use MYSQL's SUBSTRING_INDEX function. It would let you get whatever's after the last slash. Or after the second to last.
For your particular case
select
SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)
from supertext
would yield the desired result
EDIT: for update purposes
UPDATE thetable
SET thefield=SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)
I am trying to INNER JOIN two tables on a text field, but I can't get it to work.
I've backtracked to see if I can identify where my query is falling down.
I've simplified my query to use only one table, to see if I can select data from it based on a text string.
I can't make it work.
I feel like an idiot.
This is so basic, why isn't it doing it?
select * from `my-table`
where myKey = "some-text-data-that-i-copied-from-the-data-in-the-table";
Returns no rows.
"some-text-data-that-i-copied-from-the-data-in-the-table" is in field myKey. I can see it. It's in the table!!!
[if it makes any difference I am most familiar with using an Access front-end and I am porting my "skills" to MySQL because MySQL is just better really.. this seems like the most run of the mill SELECT statement ever and yet it doesn't do anything!]
EDIT:
IMSoP does this help any...
select * from mymathssowlink
where MyMathsResourceKey = 'KS2-Number-Counting and Place Value-NC3-Negative Numbers 1';
EDIT 2:
Diego Marinelli - thanks - this returns data
select * from mymathssowlink
where MyMathsResourceKey like '%KS2%';
EDIT 3:
This works...
select * from mymathssowlink
where MyMathsResourceKey like '%KS2-Number-Counting and Place Value-NC3-Negative Numbers 1%';
EDIT 4:
But this still doesn't...
select * from mymathssowlink
where MyMathsResourceKey = 'KS2-Number-Counting and Place Value-NC3-Negative Numbers 1';
puzzled
Table:
CREATE TABLE `mymathssowlink` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`ObjectiveID` varchar(13),
`MyMathsResourceKey` varchar(100),
PRIMARY KEY (`ID`),
UNIQUE KEY `idnew_table_UNIQUE` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
HOW IT GOT FIXED:
okay very weird but I have fixed the simplified problem now! I remember from my Access days that trailing spaces are the bane of matches and it seems the MySQL is a bit fiddly in this regard to. There was no problem with the data as such but the field MyMathsResourceKey was the last bit of data on each line and so not terminated with a comma. Some lines didn't have any data in that column and so were terminated with a comma rather than the MyMathsResourceKey data. I fixed it by adding a third column to my csv into which i uniformly stuck the string "PLACEHOLDER". I matched this to a column called placeholder in the table receiving the data. Now that the active (2nd column) is always terminated with a comma all the data seems to work. I can't help thinking that this is a bit odd and that MySQL data imports really don't want to be behaving like this... now to try an make my JOIN work, which how this all started!
Thanks to all, especially Rahul for the help.
... and the JOIN Works now...
Going by all the edits in your post and comments; what I feel is, there is extra space present in your column value and so you are not getting the data (since it's not matching the exact string).
So either try like
select * from mymathssowlink
where MyMathsResourceKey
like '%KS2-Number-Counting and Place Value-NC3-Negative Numbers 1%';
(OR)
Use a REPLACE() function to remove all spaces in the field value
select * from mymathssowlink
where REPLACE(MyMathsResourceKey, ' ', '')
= 'KS2-Number-Counting and Place Value-NC3-Negative Numbers 1';
You can as well use TRIM() function if you can guarantee that the spaces are present only in left/right extreme of the string.
select * from mymathssowlink
where TRIM(MyMathsResourceKey)
= 'KS2-Number-Counting and Place Value-NC3-Negative Numbers 1';
You can try Select * from table; and if that works then try select * from table where key like '%some_text%'
It could not be a problem with the sintax but with the query itself.
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 column that contains values like 5A898, 89KAS, 89ASD.
I'm trying to write a query that will only return rows where the third digit of the column is 'A'. For instance '89ASD' would be returned but '89KAS' would not. I'm not sure what the right way to go about this is. Regular expressions?
So...
SELECT column
FROM table
WHERE column = ?
WHERE column LIKE '__A%'
^^-- 2 underscores
should do the trick. two "whatever" characters, followed by an A, then followed by anything in any amount.
Maybe you can use MySQL's SUBSTRING
SELECT column
FROM table
WHERE SUBSTRING(column,3,1) = 'A'
where right(left(col,3),1) = 'A'
That can help... MarcB's answer is cleaner
You can do this with a regex, but I think it might be easier just coping with a string operation here:
SELECT column
FROM table
WHERE SUBSTRING(column,3,1) = 'A';