我的问题是 我想从tbl_one查询x行并循环结果并将值插入到其他表
中select id as user_id, p_id from users where b_id = 0 // this will give me let's say 10 rows
现在我想使用每一行插入到地址表中,如
insert into addresses (user_id, passenger_id, address_type, address_line1, city, country) value (user_id, passenger_id, 'Test street address', 'XXX', "XX")
我怎么能在mysql中做到这一点。尝试在https://dev.mysql.com/doc/refman/5.7/en/loop.html的mysql文档中搜索,但解释的循环逻辑非常静态,我不明白。
答案 0 :(得分:0)
使用INSERT INTO SELECT语句:
INSERT INTO addresses (user_id, passenger_id, address_type, address_line1, city, country)
SELECT id, p_id, address_type, 'Test street address', 'XXX', 'XX')
FROM users
WHERE b_id = 0
答案 1 :(得分:0)
如果您想将其他列如address_type等保持为静态,那么您可以使用以下查询:
{{1}}