使用内部联接插入查询 [英] Insert query with an inner join

查看:85
本文介绍了使用内部联接插入查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要进行插入查询,该查询具有对用户数据表的内部联接.

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

表的示例如下:

users:

uid | name 


users_data:

id | data_id | uid | other fields


data_stocks

id | data_id | quantity


因此,我试图插入data_stocks中,这仅与从users中了解uid有关.


So im trying to insert into data_stocks relating to only knowing the uid from users.

像这样的金达:

INSERT INTO data_stocks (data_id,quantity)
VALUES (' need to join it some how ','$quantity');

这在MySQL中可行吗?

Is this possible in mySQL?

推荐答案

您要使用语句的insert ... select形式:

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;

这篇关于使用内部联接插入查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆