简体   繁体   中英

Copy/ Update the primary keys value to another column of the same table

I have a MySQL database table subscribers as shown below:

all i need is make the subscriber_number same as subscriber_id.

Eg: If subscriber_id is 261 and subscriber_number is 262 , then i need to make subscriber_number as 261, same as the subscriber id.

Could you please help me with MySQL query to sort this out ?

UPDATE 1:

am looking to get MySQL query from you experts here. what i tried was to get the rows where both id and number are different

SELECT subscriber_id,subscriber_number 
FROM subscribers 
WHERE subscriber_id <> subscriber_number

Please run the code snippet below to view the table structure with sample data

 <table> <thead> <tr> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">subscriber_id</td> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">subscriber_number</td> </tr> </thead> <tbody> <tr> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">260</td> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">260</td> </tr> <tr> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">261</td> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">262</td> </tr> <tr> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">262</td> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">264</td> </tr> <tr> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">263</td> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">272</td> </tr> <tr> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">264</td> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">274</td> </tr> <tr> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">266</td> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">277</td> </tr> <tr> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">268</td> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">282</td> </tr> <tr> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">269</td> <td style="border-right:1px solid #000;border-bottom:1px solid #000;">269</td> </tr> </tbody> </table> 

The SQL would be

UPDATE subscribers SET subscriber_number = subscriber_id

This will set the subscriber_number to subscriber_id for every single row in your subscriber-table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM