sql1 = " SELECT a.biz_date, a.county_name, a.depart_name, a.person_uuid, a.cust_code, a.cust_name, a.terminal_level_name, a.data_gather_type_name, a.sc_type_name, a.sc_score, b.sc_type_name AS sc_type_name_old, b.sc_score AS sc_score_old, a.sc_score-b.sc_score AS up FROM ads_yh_yx_sctwo a LEFT JOIN ads_yh_yx_sctwo b ON a.cust_code = b.cust_code AND b.biz_date = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 5 DAY), '%Y%m%d') AND b.person_uuid = '00000000001389361632876_5304_371' WHERE a.biz_date = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 DAY), '%Y%m%d') AND a.person_uuid = '00000000001389361632876_5304_371' AND a.sc_type_name != b.sc_type_name AND (a.sc_score - b.sc_score) <> 0; "; $result1 = mysqli_query($conn1, $sql1); 如何把查出来的多条记录再插入对应的表里
时间: 2024-04-15 08:28:53 浏览: 122
A-wise-man-live.zip_4 3 2 1
你可以使用INSERT INTO SELECT语句将查询结果插入到另一个表中。在这种情况下,你需要创建一个INSERT语句,将查询结果的列与目标表的列对应起来。
假设你要将查询结果插入到名为"target_table"的表中,你可以像这样修改你的代码:
```python
$sql2 = "
INSERT INTO target_table (biz_date, county_name, depart_name, person_uuid, cust_code, cust_name, terminal_level_name, data_gather_type_name, sc_type_name, sc_score, sc_type_name_old, sc_score_old, up)
SELECT
a.biz_date,
a.county_name,
a.depart_name,
a.person_uuid,
a.cust_code,
a.cust_name,
a.terminal_level_name,
a.data_gather_type_name,
a.sc_type_name,
a.sc_score,
b.sc_type_name AS sc_type_name_old,
b.sc_score AS sc_score_old,
a.sc_score - b.sc_score AS up
FROM ads_yh_yx_sctwo a
LEFT JOIN ads_yh_yx_sctwo b ON a.cust_code = b.cust_code
AND b.biz_date = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 5 DAY), '%Y%m%d')
AND b.person_uuid = '00000000001389361632876_5304_371'
WHERE a.biz_date = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 DAY '%Y%m%d')
AND a.person_uuid = '00000000001389361632876_5304_371'
AND a.sc_type_name != b.sc_type_name
AND (a.sc_score - b.sc_score) <> 0;
";
$result2 = mysqli_query($conn2, $sql);
```
上述代码将查询结果插入到名为"target_table"的表中,确保目标表的列与查询结果的列一一对应。注意,你需要将$conn1替换为$conn2来执行INSERT语句。
请确保在执行INSERT操作之前,你已经创建了目标表并且列的顺序和数据类型与查询结果一致。此外,还要确保连接到正确的数据库实例($conn2)。
希望对你有所帮助!如果还有其他问题,请随时提问。
阅读全文