I have a MySQL table including following columns :
+------------+-------------+
| auto_no | auto_no_new |
+------------+-------------+
| 2021-10431 | 20577 |
| 2021-10432 | 20578 |
| 2021-10433 | 20579 |
| 2021-10434 | 20580 |
| 2021-10435 | 20581 |
| 2021-10436 | 20582 |
+------------+-------------+
Value in the "auto_no" column increments with relevant year. The values show in the table started previously and changed at the beginning of the year as above. Then I needs to start the values in the "auto_no" columns as follows :
+------------+-------------+
| auto_no | auto_no_new |
+------------+-------------+
| 2021-00001 | 20577 |
| 2021-00002 | 20578 |
| 2021-00003 | 20579 |
| 2021-00004 | 20580 |
| 2021-00005 | 20581 |
| 2021-00006 | 20582 |
+------------+-------------+
I used following query
update table set auto_no LIKE %'Y'- '????1'% where auto_no_new > 20577
But didn't get the desired output. What may be going wrong ? Can anyone help ?
Seems trivial
drop table if exists t;
create table t(auto_no varchar(12), auto_no_new int);
insert into t values
( '2021-10431' , 20577 ),
( '2021-10432' , 20578 ),
( '2021-10433' , 20579 ),
( '2021-10434' , 20580 ),
( '2021-10435' , 20581 ),
( '2021-10436' , 20582 );
update t
set auto_no = concat(substring_index(auto_no,'-',1),'-',lpad(auto_no_new - 20576,5,'0'))
where substring_index(auto_no,'-',1) = 2021;
select * from t;
+------------+-------------+
| auto_no | auto_no_new |
+------------+-------------+
| 2021-00001 | 20577 |
| 2021-00002 | 20578 |
| 2021-00003 | 20579 |
| 2021-00004 | 20580 |
| 2021-00005 | 20581 |
| 2021-00006 | 20582 |
+------------+-------------+
6 rows in set (0.013 sec)
or if you don't know the min(auto_no_new)
update t cross join(select min(auto_no_new) - 1 minno from t where substring_index(auto_no,'-',1) = 2021) s
set auto_no = concat(substring_index(auto_no,'-',1),'-',lpad(auto_no_new - s.minno,5,'0'))
where substring_index(auto_no,'-',1) = 2021;
After doing a few tests, this is what I come up with:
UPDATE table1 CROSS JOIN (SELECT #rn := 0) R
SET auto_no=CONCAT(SUBSTRING(auto_no,1,LOCATE('-',auto_no)),LPAD(#rn := #rn+1,5,0))
WHERE auto_no LIKE '2021-%';
However, I advise you to please don't run the update query without backing up your table first. I think the best way is for you to create a copy of the original table and run the update query over it rather than doing it on the original table. At least that would give you chance to do-over if something went wrong. Also, once you're satisfied with the end result (after the update), you can simply rename the original table to something like table1_original then rename the copy table as the original table.
Here is a fiddle demo
Related
I'm trying to update this table (name: sports_club):
+----+-----------+----------------+
| id | name | sport_interest |
+----+-----------+----------------+
| 7 | Winston | NULL |
| 8 | Winnefer | NULL |
| 9 | Winsteen | NULL |
| 10 | Wincifer | NULL |
| 11 | Winster | NULL |
| 12 | Winstonia | NULL |
| 13 | Wilson | NULL |
| 14 | Winnerva | NULL |
| 15 | WinDiesel | NULL |
+----+-----------+----------------+
I'm trying to add values to just the sport_interest column of the existing records, I tried to do that by using:
insert into sport_interest values ('cricket'), ('football'), etc.
But it did not work as it was trying to add it to new records which I don't want to do.
I'm using MySQL
You can do the updates manually like:
update sport_interest set sport_interest = 'cricket' where id=7;
update sport_interest set sport_interest = 'football' where id=8;
And so on...
But, if you have the id of the values that you need to update it is much easier.
You can create another table like:
create table sport_interest_bak (
id int ,
sport_interest varchar(50));
You can use LOAD DATA to insert the data on the new table and then update using inner join, it is simpler and much faster.
update sport_interest s
inner join sport_interest_bak sb on s.id=sb.id
set s.sport_interest=sb.sport_interest;
I suggest reading https://www.mysqltutorial.org/mysql-basics/ or https://www.techonthenet.com/mysql/index.php this are much better than W3Schools
Use the 'Update' command for existing entries.
"UPDATE table SET sport_interest = 'football'" should work.
Use WHERE clause if you did not want all cells to update. ex.
UPDATE table SET sport_interest = 'football' WHERE name = 'Winston'
+----+------------+------------+------------+----------+
| id | phone_no | join_date | city | blood_gp |
+----+------------+------------+------------+----------+
| 1 | 80077672xx | 1997-07-19 | Delhi | NULL |
| 2 | 80077642xx | 1998-07-19 | New Delhi | NULL |
| 3 | 80477642xx | 1999-07-19 | Mumbai | NULL |
| 4 | 80077654xx | 1997-05-31 | Kolkata | NULL |
+----+------------+------------+------------+----------+
I want to enter all the blood groups at once . Is there a way to do so ?
you can use single query with select and update
UPDATE table1 , (SELECT * FROM table2 where 1) src
SET table1.blood_gp = src.filed2 where 1 ;
if you want to insert multiple row data using single query then use this code
INSERT INTO yourtable (x,y,z) VALUES (a1,a2,a3), (b1,b2,b3);
or if you want to update one column value all filed then use this code
update yourtable set blood_gp = 'yourvalue' where 1;
if any problem then inform me
Just make an update query without where clause.
update table set blood_gp = 'value'
That's generalize query.
I want to select value from table sorted by a certain order.
I have a table called test that looks like this:
| date | code | value |
+----------+-----------+----------+
| 20050104 | 000005.SZ | -6359.19 |
| 20050104 | 600601.SH | -7876.34 |
| 20050104 | 600602.SH | -25693.3 |
| 20050104 | 600651.SH | NULL |
| 20050104 | 600652.SH | -15309.9 |
...
| 20050105 | 000005.SZ | -4276.28 |
| 20050105 | 600601.SH | -3214.56 |
...
| 20170405 | 000005.SZ | 23978.13 |
| 20170405 | 600601.SH | 32212.54 |
Right now I want to select only one date, say date = 20050104, and then sort the data by a certain order (the order that each stock was listed in the stock market).
I have another table called stock_code which stores the correct order:
+---------+-----------+
| code_id | code |
+---------+-----------+
| 1 | 000002.SZ |
| 2 | 000004.SZ |
| 3 | 600656.SH |
| 4 | 600651.SH |
| 5 | 600652.SH |
| 6 | 600653.SH |
| 7 | 600654.SH |
| 8 | 600602.SH |
| 9 | 600601.SH |
| 10 | 000005.SZ |
...
I want to sorted the selected data by stock_code(code_id), but I don't want to use join because it takes too much time. Any thoughts?
I tried to use field but it gives me an error, please tell me how to correct it or give me an even better idea.
select * from test
where date = 20050104 and code in (select code from stock_code order by code)
order by field(code, (select code from stock_code order by code));
Error Code: 1242. Subquery returns more than 1 row
You told us that you don't want to join because it takes too much time, but the following join query is probably the best option here:
SELECT t.*
FROM test t
INNER JOIN stock_code sc
ON t.code = sc.code
WHERE t.date = '20050104'
ORDER BY sc.code_id
If this really runs slowly, then you should check to make sure you have indices setup on the appropriate columns. In this case, indices on the code columns from both tables as well as an index on test.date should be very helpful.
ALTER TABLE test ADD INDEX code_idx (code)
ALTER TABLE test ADD INDEX date_idx (date)
ALTER TABLE code ADD INDEX code_idx (code)
I want to merge two rows into one.The below format is in the database.
+----+---------+-----------------------+-------------------------+
| id | appid | photo | signature |
+====+=========+=======================+=========================+
| 1 | 10001 | 10001.photograph.jpg | NULL |
| 2 | 10001 | NULL | 10001.signature.jpg |
+----+---------+-----------------------+-------------------------+
I want a mysql query so that i can fetch data like below,
+--------+------------------------+-------------------------+
| appid | photo | signature |
+========+========================+=========================+
|10001 | 10001.photograph.jpg | 10001.signature.jpg |
+--------+------------------------+-------------------------+
Kindly suggest...
You can also use max function
select appid,
max(photo) photo,
max(signature) signature
from test
group by appid
Demo
This should do this:
select t1.appid,t1.photo,t2.signature from mytable t1 join mytable t2 on t1.appid=t2.appid where t1.id=1 and t2.id=2
I am trying to populate an empty table(t) from another table(t2) based on a flag field being set. He is my attempt below and the table data.
UPDATE 2014PriceSheetIssues AS t
JOIN TransSalesAvebyType2013Combined AS t2
SET t.`Tran_Type`=t2.`Tran_Type` WHERE t.`rflag`='1';
When I run the script, I receive (0) zero records affected.??
+-----------+----------------+-------------------+-------+-------+
| Tran_Type | RetailAvePrice | WholesaleAvePrice | Rflag | Wflag |
+-----------+----------------+-------------------+-------+-------+
| 125C | 992 | 650 | 1 | NULL |
| 2004R | 1500 | NULL | 1 | NULL |
| 4EAT | 1480 | 1999 | 1 | 1 |
+-----------+----------------+-------------------+-------+-------+
I think you should just do the following
INSERT INTO 2014PriceSheetIssues
( `fldX`, `fldY` )
VALUES (
SELECT `fldX`, `fldY`
FROM TransSalesAvebyType2013Combined
WHERE 2014PriceSheetIssues.`rflag`='1'
)
The select query gets the values and the insert puts it in the (empty) other table.