Home  >  Q&A  >  body text

Insert modified value of one field into new field in SQL

My problem is that I know nothing about SQL queries, but need to do something that I think shouldn't be difficult. I've tried some of the recommended questions but I just can't really understand how to extrapolate it to my case, hope you can help me.

Basically what I need is to create some rows in the table and use Values ​​from other rows in the same table, I'd try to be very specific.


I have a table (vb_postmeta) with a lot of rows, some of them look like this:

meta_id post_id MetaKey 元value
1434 500 bpxl_videourl 1006hyOKoUM
1435 501 bpxl_videourl skjd9F008mN

What I need here is, for each row, create a new row like this:

meta_id post_id MetaKey 元value
automatic 500 csco_post_video_url https://www.youtube.com/watch?v=lOO6hyOKoUM
automatic 501 csco_post_video_url https://www.youtube.com/watch?v=skjd9F008mN

meta_id is the key and therefore should be automatically generated, while post_id should be the same and meta_value should also have the same value with the Youtube URL appended in front.

I really don’t know how difficult this is, but I hope it’s not too hard,

Thanks in advance for any help, been struggling with this.

P粉790187507P粉790187507213 days ago386

reply all(2)I'll reply

  • P粉696891871

    P粉6968918712024-03-21 09:40:23

    Assuming your meta_id is AI (auto-increment) and using mySQL (minor modification of MSSQL), try:

    INSERT INTO vb_postmeta (
    SELECT null,
    post_id,
    'csco_post_video_url'
    CONCAT('https://www.youtube.com/watch?v=',meta_value
    FROM vb_postmeta)

    reply
    0
  • P粉151720173

    P粉1517201732024-03-21 09:06:38

    Based on the suggested answers, I ended up using this:

    INSERT INTO vb_postmeta(
        SELECT
            NULL,
            post_id,
            'csco_post_video_url',
            CONCAT('https://www.youtube.com/watch?v=', meta_value)
        FROM
            vb_postmeta
        WHERE
            meta_key = 'bpxl_videourl'
    )

    reply
    0
  • Cancelreply