MYSQL command:
UPDATE `tbl_objednavka` SET
`TOTAL` = '6300',
`EANS` = CAST('8433611369655;' AS char)+CAST(`EANS` AS char),
`COUNTS` = CAST('1;' AS char)+CAST(`COUNTS` AS char)
WHERE `ID_OBJEDNAVKA`=2;
_____________________________________
| | |
| EANS | COUNTS |
+-----------------+-----------------+
| | |
| 8433611364094 | 1 |
+-----------------+-----------------+
It for some weird reason '8433611369655;' dont merge strings but add one number to the other so I get something as this: 1.6867223e+13...
I need to get an array, so this: 8433611369655;8433611364094 in EANS and 1;1 in COUNTS
I can use php for this, but I would love to do this using SLQ only
You are using MySQL, so you want to use concat() rather than + for string concatenation.
Also, you should never convert values to char() without a length. In this case, though, I think varchar() would be a more appropriate type.
However, I would suggest this query:
UPDATE `tbl_objednavka`
SET `TOTAL` = '6300',
`EANS` = concat('8433611369655;', `EANS`),
`COUNTS` = concat('1;', `COUNTS`)
WHERE `ID_OBJEDNAVKA`=2;
You don't seem to need the casts at all. The types of EANS and COUNTS should be character to begin with, because you are assigning character values to them.
If they are numeric, then you need to alter the table so they can hold the values you want. In practice, I would suggest adding new columns in this case. Or, using a view to create the new columns.
UPDATE `tbl_objednavka` SET
`TOTAL` = '6300',
`EANS` = CONCAT('8433611369655;', CAST(`EANS` AS char)),
`COUNTS` = CONCAT('1;', CAST(`COUNTS` AS char))
WHERE `ID_OBJEDNAVKA`=2;
MySQL doesn't use + for string concatenation, you have to use the CONCAT() function.
Related
I would like to create a new column in a MYSQL table based on the string values in an existing column.
My strategy is to first create an empty column and then update the values in the new column based on values in the existing column. However, I am stumbling on how to parse the string in order to extract the correct values.
The string is of the form 1.1.25. I want to extract the value before the first period and the value between the two periods and put these in new columns.
mytable
id|actsceneline|text
1 |1.1.1 |How are you.
1 |1.1.2 |Not bad. You?
To create the new empty column
ALTER TABLE mytable
ADD COLUMN act VARCHAR(6) NOT NULL,
ADD COLUMN scene VARCHAR(6) NOT NULL
To change the values in the new columns, I imagine I would do something like:
UPDATE mytable SET act = '1',scene = 1
And then use MYSQL string functions such as instr or substr or regex to extract the values and update the new columns as in.
UPDATE mytable SET act =
SELECT SUBSTR(actsceneline, 1, LOCATE('.', text)) FROM mytable
However, I'm struggling with how to extract the values from the string.
Thanks for any suggestions.
Try using SUBSTRING_INDEX():
UPDATE mytable
SET act = SUBSTRING_INDEX(actsceneline, '.', 1),
scene = SUBSTRING_INDEX(SUBSTRING_INDEX(actsceneline, '.', 2), '.', -1);
Result given your data:
mysql> select * from mytable;
+----+--------------+---------------+-----+-------+
| id | actsceneline | text | act | scene |
+----+--------------+---------------+-----+-------+
| 1 | 1.1.1 | How are you. | 1 | 1 |
| 2 | 1.1.2 | Not bad. You? | 1 | 1 |
+----+--------------+---------------+-----+-------+
Best way to create a select and what you want to update.
create a new table from your existing table.
"create table destinationtablename
select * from sourcetable;"
then work on your destinationtablename.
All work finished then check twice before update to original table or you can also take backup of your data by creating new table.
I'm was trying to make function that will return random character. What I have is a string that contains alphanumerics. In Postgresql I can set text as array, but I don't know how MySQL do same. for example:
in postgresql
DECLARE chars TEXT[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
So when I want get chars[10] it will return A. How to do this in MySQL?
I think the questoin is not about arrays, it is about get random char, right? In this case You can use next approach in MySQL:
SELECT SUBSTRING(
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
ROUND(RAND()*61)+1,
1
) as random_char;
The SUBSTRING function returns one char from random position.
Try it on SQLize.online
Starting MySQL 5.7, one option is to use a JSON array:
set #chars = '["0", "1", "A", "B"]';
select json_unquote(json_extract(#chars, '$[2]')) as value_at_index_2;
Yields:
| value_at_index_2 |
| :--------------- |
| A |
I am trying to remove part of a string. But can't do it properly. String is like this: 4,290TL〜9,490TL So trying to remove after this 〜
I tried
UPDATE SET price = SUBSTRING_INDEX(price, '〜')
But not worked.
SUBSTRiNG_INDEX requires 3 parameters, the last one being the delimiter count. In this case you need to supply a count of 1, indicating that you want everything to the left of the first occurrence of the delimiter 〜. Additionally, you need to specify your table name in the query. Try this:
UPDATE yourtable SET price = SUBSTRING_INDEX(price, '〜', 1)
UPDATE SET price = SUBSTRING_INDEX(price, '〜', 1)
Please note the the strings you have shared with this question or comments uses DIFFERENT wavy line ("tilde") characters
# the tilde character used here is DIFFERENT to the next example
select substring_index('4,290TL〜9,490TL','〜',1)
;
+----+------------------------------------------------------+
| | substring_index('4,290TL〜9,490TL','〜',1) |
+----+------------------------------------------------------+
| 1 | 4,290TL |
# the tilde character is different to the one above
select substring_index('18,990万円(1戸)~28,790万円(1戸)','~',1)
;
+----+-----------------------------------------------------------+
| | substring_index('18,990万円(1戸)~28,790万円(1戸)','~',1) |
+----+-----------------------------------------------------------+
| 1 | 18,990万円(1戸) |
You will need to be CERTAIN the the tilde you use as delimiter is the one you use in substring_index() otherwise that function will just return the whole string.
We currently have a database that has stored over 100,000 records of data over the years however in a structure that does not work anymore.
There is a field in the table called youtube_video
It has been storing all of the embed YouTube videos like this:
http://www.youtube.com/embed/3mHuu5NklOs?rel=0
http://www.youtube.com/embed/3mHuu5NklOs
We need to change it to:
https://www.youtube.com/watch?v=3mHuu5NklOs
Is there a way to write a query that makes this change with a single query?
You can use REGEXP_REPLACE :
SELECT REGEXP_REPLACE(
youtube_video,
'^http://www.youtube.com/embed/([^?]+).*',
'https://www.youtube.com/watch?v=\1'
) FROM mytable
Regex breakdown :
^ : start of string
http://www.youtube.com/embed/ : constant string part
([^?]+) : as many consecutive characters as possible others than a question mark ; the surrounding parentheses capture that part of the string, and make it available as \1 in the second argument to REGEXP_REPLACE()
.* : anything (until end of string)
This demo on DB Fiddle returns :
| youtube_video | new_youtube_video |
| ---------------------------------------------- | ------------------------------------------- |
| http://www.youtube.com/embed/3mHuu5NklOs?rel=0 | https://www.youtube.com/watch?v=3mHuu5NklOs |
| http://www.youtube.com/embed/3mHuu5NklOs | https://www.youtube.com/watch?v=3mHuu5NklOs |
If needed, you can easily turn this into an UPDATE :
UPDATE mytable
SET youtube_video = REGEXP_REPLACE(
youtube_video,
'^http://www.youtube.com/embed/([^?]+).*',
'https://www.youtube.com/watch?v=\1'
);
I have a query I need to run on almost 2000 strings where it would be very helpful to be able to do a list like you can with the "IN" operator but using the LIKE comparison operation.
For example I want to check to see if pet_name is like any of these (but not exact): barfy, max, whiskers, champ, big-D, Big D, Sally
Using like it wouldn't be case sensitive and it can also have an underscore instead of a dash. Or a space. It will be a huge pain in the ass to write a large series of OR operators. I am running this on MySQL 5.1.
In my particular case I am looking for file names where the differences are usually a dash or an underscore where the opposite would be.
For this task I would suggest making use of RegExp capabilities in MySQL like this:
select * from EMP where name RLIKE 'jo|ith|der';
This is case insensitive match and will save from multiple like / OR conditions.
You could do something like this -
SELECT FIND_IN_SET(
'bigD',
REPLACE(REPLACE('barfy,max,whiskers,champ,big-D,Big D,Sally', '-', ''), ' ', '')
) has_petname;
+-------------+
| has_petname |
+-------------+
| 5 |
+-------------+
It will give a non-zero value (>0) if there is a pet_name we are looking for.
But I'd suggest you to create a table petnames and use SOUNDS LIKE function to compare names, in this case 'bigD' will be equal to 'big-D', e.g.:
SELECT 'bigD' SOUNDS LIKE 'big-D';
+---------------------------+
| 'bigD'SOUNDS LIKE 'big-D' |
+---------------------------+
| 1 |
+---------------------------+
Example:
CREATE TABLE petnames(name VARCHAR(40));
INSERT INTO petnames VALUES
('barfy'),('max'),('whiskers'),('champ'),('big-D'),('Big D'),('Sally');
SELECT name FROM petnames WHERE 'bigD' SOUNDS LIKE name;
+-------+
| name |
+-------+
| big-D |
| Big D |
+-------+
As first step put all static values in any temporary table, this would be lookup dictionary.
SELECT * FROM Table t
WHERE EXISTS (
SELECT *
FROM LookupTable l
WHERE t.PetName LIKE '%' + l.Value + '%'
)
Configure the column containing those 2000 values for full-text searching. Then you can use MySQL's full-text search feature. Refer to their docs
You could use REGEXP instead. It worked like a charm for me
pet_name regexp 'barfy|max|whiskers|champ|you name it'