user3716007
user3716007

Reputation: 23

Make a single insert statement

I have a MySQL table named stock_sales which has 3 columns. I want to insert data with a single statement. Two statements do not work as expected:

"INSERT INTO  stock_sales (size) SELECT size FROM stock_avail WHERE id='5957';
"INSERT INTO  stock_sales (transactionid, date) VALUES ('$bill','$date')";

How can I do it in one statement and insert the data into a single row?

Upvotes: 2

Views: 50

Answers (2)

Paul Spiegel
Paul Spiegel

Reputation: 31832

Try:

INSERT INTO  stock_sales (size, transactionid, date) 
    SELECT size, '$bill', '$date' 
    FROM stock_avail 
    WHERE id='5957'

or

INSERT INTO  stock_sales (transactionid, date, size) 
    VALUES ('$bill', '$date', (SELECT size FROM stock_avail WHERE id='5957'))

Upvotes: 3

Nana Partykar
Nana Partykar

Reputation: 10548

$sqlQuery = "INSERT INTO  stock_sales (size) SELECT size FROM stock_avail WHERE id='5957';";
$sqlQuery. = "INSERT INTO  stock_sales (transactionid, date) VALUES ('$bill','$date')";

mysqli_multi_query($con,$sqlQuery);

The mysqli_multi_query() function performs one or more queries against the database. The queries are separated with a semicolon.

For more info, please click Mysqli_Multi_Query - W3 Schools

Upvotes: 2

Related Questions