1

is it possible to combine the update and insert query in same statement apart from transactions and stored procedure.

For ex:At a time i want to update the records in table1 as well as insert the data in table2.

2 Answers 2

2

It is possible using

multi_query

You can fin more about it here

Something like this

 $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");
Sign up to request clarification or add additional context in comments.

2 Comments

can you tel me sample query
i need sample query bcoz i tried many ways.but its not working.
0

You can just type it and automatically mysqli will read it. like

@mysql_query("INSERT INTO x (`name`,`age`) VALUES ('me','21')");
@mysql_query("UPDATE y SET `status`='verified' WHERE `name`='me'");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.