Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8757345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:16:46+00:00 2026-06-13T14:16:46+00:00

I have query that INSERTS both explicit values and SELECTd content. I’m also doing

  • 0

I have query that INSERTS both explicit values and SELECTd content. I’m also doing basic incrementing.

INSERT INTO `table` (`myID`, `myVal1`, `myVal2`) SELECT `myID` + 1, 'explValHere', 'otherValThere')
FROM `table` ORDER BY `myID` DESC LIMIT 0,1

I am doing this as the table has multiple id’s and incrementing within a specific column. So I can’t, as you would first say, use auto incrementing and insert_id.

The problem of course is the insert doesn’t return the select, but can it? Is there a way of running this insert query and returning any of the result?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:16:47+00:00Added an answer on June 13, 2026 at 2:16 pm

    Since your query has a LIMIT 1 you could store the “result” in a session/user-defined variable. Still two queries but reentrant; each connection is its own session.

    <?php
    $pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    setup($pdo);
    
    $query = "
        INSERT INTO tbl1 (myID, myVal1, myVal2)
        SELECT @foo:=myID+1, 'val1', 'val2' FROM tbl2 WHERE x=0 LIMIT 1
    ";
    $pdo->exec($query);
    foreach( $pdo->query('SELECT @foo as foo') as $row ) {
        echo $row['foo'];
    }
    
    
    function setup($pdo) {
        $pdo->exec('CREATE TEMPORARY TABLE tbl1 (myID int, myVal1 varchar(16), myVal2 varchar(16))');
        $pdo->exec('CREATE TEMPORARY TABLE tbl2 (myID int, x int)');
        $pdo->exec('INSERT INTO tbl2 (myID, x) VALUES (1,1),(2,1),(3,0),(4,0),(5,1)');
    }
    

    the “first” record in tbl2 having x=0 is (myID=3,x=0) and the script prints 4.

    Other than that and stored procedures et al there’s (to my knowledge) nothing like SQLServer’s OUTPUT or postgresql’s RETURNING clause for MySQL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query that inserts rows, using a subquery like so: INSERT INTO
I have an INSERT query that is pulling data from two tables and inserting
I have an insert query that gets generated like this INSERT INTO InvoiceDetail (LegacyId,InvoiceId,DetailTypeId,Fee,FeeTax,Investigatorid,SalespersonId,CreateDate,CreatedById,IsChargeBack,Expense,RepoAgentId,PayeeName,ExpensePaymentId,AdjustDetailId)
I have table that I insert data with following query (from c# code): INSERT
I have an INSERT query in Oracle 10g that is getting stuck on a
I have a MySQL insert statement that inserts data from an existing Despgoods table
I have an INSERT query that works fine, but when I add an extra
I have a query that's written dynamically (OO PHP via Joomla) to insert some
In a windows application I have a query that im trying to insert the
I am using in C# MYsql .I have query that works if I run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.