Insert query with an inner join

前端 未结 1 1641
生来不讨喜
生来不讨喜 2021-02-20 04:09

I want to make my insert query which has an inner join to a user\'s data table.

The example of the tables is like this:

users:

uid |         


        
1条回答
  •  闹比i
    闹比i (楼主)
    2021-02-20 04:49

    You want to use the insert ... select form of the statement:

    INSERT INTO data_stocks (data_id,quantity)
        select ud.data_id, $quantity
        from users u join
             users_data ud
             on u.uid = ud.uid;
    

    If you are doing this for only one user, it might look more like this:

    INSERT INTO data_stocks (data_id,quantity)
        select ud.data_id, $quantity
        from users_data ud
        where ud.uid = $uid;
    

    0 讨论(0)
提交回复
热议问题