Results 1 to 2 of 2

Thread: How to use a value from the same column in a new insert

  1. #1
    Join Date
    Dec 2013
    Melbourne, Australia

    How to use a value from the same column in a new insert

    I have an SQL statement which inserts numerous rows of data from an array (in PHP).

    The data is updated daily with new values and assigned a new report_id.

    there is a field labelled 'hrs_offset' in the same table (ReportData) which I want to carry across into this statement - so basically, if the report_id = 50 and the last report_id = 45, I want to copy across all the values from ReportData.hrs_offset WHERE report_id = 45 AND tech=tech.

    Is this achievable? Or should I just get the values in a separate statement and load them into the PHP array?

    INSERT INTO ReportData 
    	(report_id, line_type, tech, name, rate, open_wip, clock_hrs, close_wip, avail_hrs, sold_hrs, sold_amt, cost_hrs, cost_amt, labour_gross, margin, docs, unprod_hrs, sold_avail, clock_status)
    	('".$this->reportID."', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

  2. #2
    Join Date
    Aug 2004
    Not sure if this answers your need, but you can use a select to populate the values of an insert, e.g.:
    PHP Code:
    $sql "
    INSERT INTO table_name (col1, col2, col3, col4) 
    SELECT '
    {$new_value}', col2, col3, col4 FROM table AS t2 
    WHERE t2.col4='something'"

    (might have to fiddle around with the syntax a bit depending on the specific DBMS, but that's the general idea)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center

Recent Articles