PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 23, 2022

[FIXED] How to insert a date to new column in table 2 based on product_Id and match the (product_id) in table 1 and use a column (date) to add to table 2

 October 23, 2022     mysql, sql, sql-update     No comments   

Issue

Table 1

Id place expiry_date
10 xyz 2022-09-12

Table 2 - expiry_date is the new column created in table 2. Need to fetch expiry date from table 1 where T1_id (in table 2) matches id (in table 1)

Oid userid expiry_date T1_id
2 123 10

How to fetch expiry date (table 1 and fill the new column in table 2) only if the T1_id and Id(table 1) matches

Trying

insert into (sql) statements 
Joins used 
Join table1.Id on table2.T1_id 

Solution

Insert statements won't allow you to change values from an existing table, they only allow you to add brand new rows to a table. In your case you may want to use an UPDATE statement.

In order to get matches between the two tables, you can apply a JOIN operation within the UPDATE statement, using the condition you pointed in your post description.

UPDATE     tab2 
INNER JOIN tab1
        ON tab1.Id = tab2.T1_id
SET tab2.expiry_date = tab1.expiry_date;

Check the demo here.



Answered By - lemon
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing