将两个表中的值插入到一个表中 [英] Insert values from two tables into one table

查看:64
本文介绍了将两个表中的值插入到一个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,新场景继续基本问题如下:

Now, new scenario in continuation to Base Question is as below:

表格:menu_list_hed

menu_id    menu_name    shop_id    menu category
--------------------------------------------
 1        Breakfast      12          Veg
 2        Dinner         12          Veg
 .... Many more
 85       Breakfast      13          Veg
 36       Dinner         13          Veg
 .... Many more

我有另一个名为 menu_items 的表,它已经有 shop_id 12 的商品

I have another table called menu_items which already has items for shop_id 12

id     shop_id    menu_id   item_name
--------------------------------------
 1      12           1       Roti
 2      12           1       Prantha
 3      12           2       Thali
 4      12           2       Sweet
 .... many More

现在我希望为 shop_id 13 插入商品,因为 items 相同,但 shop_idmenu_id会有所不同,如下所示:

Now i want the items to be inserted for shop_id 13 because items are same but shop_id and menu_id will be different as below:

  id     shop_id    menu_id   item_name
--------------------------------------
 1      12           1       Roti
 2      12           1       Prantha
 3      12           2       Thali
 4      12           2       Sweet
 .... many More
 111     13          85      Roti
 112     13          85      Prantha
 113     13          36      Thali
 114     13          36      Sweet

我通过在上一个问题中使用查询复制值,然后像 as 一样运行更新语句

i copied values by using query in previous question and then running update statement like as

UPDATE item  SET `menu_id`= 85 where `menu_id` = 1 and restaurant_id = 13;

但我想通过单个查询实现它

but i want to achieve it with single query

推荐答案

INSERT INTO menu_items (shop_id, menu_id, item_name) 
SELECT 13,  menu_id + 24,  item_name FROM  menu_items 

这篇关于将两个表中的值插入到一个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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