yupang
yupang

Reputation: 155

How to insert data from one table to another with sql

I have three tables

one

    id         thekey        invitedby
    1          sfdsaf           jkfd
    2          fdsalkj           dd
    3           kjk              
    4           ppfdaf           0
    5           fdslkj           op
    6            dsalk           ww
    7           fdsadff          xx
    8            lkjlk           0
    9            klkj            0

gx

    id      invited      inviter        cost
    1         2             dd           1500
    2         5             op           1200
    3         6             ww           1000
    4         7             xx           1212

dkk

    id        charge       thekey

I try to insert into dkk, the rule is somebody who invited others will be insert into table dkk, and gx.id < 4, at last the data in table dkk like following

    id        charge       thekey
    1         10            sfdsaf
    2         10            kjk
    3         10            fdslkj

The id should be inserted in table one is 1,3,5, my sql query is

    insert into `dkk` (charge,thekey)
        select 20,thekey
        from one o where exists (select 1 from gx g where g.inviter = o.invitedby)

But this insert the id 2,5,6, the people is invited, not the inviter, any ideas?

Upvotes: 0

Views: 67

Answers (1)

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 31993

insert into dkk (charge,thekey)
select 10, thekey from one o inner join gx g on o.invitedby=g.inviter
where g.id<4

Upvotes: 1

Related Questions