是否可以在事务和存储过程之外的同一语句中组合更新和插入查询。
例如:我想更新table1中的记录以及在table2中插入数据。
答案 0 :(得分:2)
可以使用
multi_query
您可以找到有关它的更多信息here
像这样的东西
$link = mysqli_connect("server", "user", "pass", "db");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$testquery .= "INSERT INTO x (`agent_name`, `job_number`, `job_value`, `points_value`) SELECT agent_name, job_number, job_value, points_value FROM jobs WHERE YEAR(booked_date) = $current_year && WEEKOFYEAR(booked_date) = $weeknum;";
$testquery .= "SELECT agent_name, SUM(job_value), SUM(points_value) FROM leaderboard GROUP BY agent_name ORDER BY SUM(points_value) DESC";
mysqli_multi_query($link, $testquery) or die("MySQL Error: " . mysqli_error($link) . "<hr>\nQuery: $testquery");
答案 1 :(得分:0)
你可以输入它并自动mysqli将读取它。像
@mysql_query("INSERT INTO x (`name`,`age`) VALUES ('me','21')");
@mysql_query("UPDATE y SET `status`='verified' WHERE `name`='me'");