im using php as my language, and oracle xe for my database.
i have table Order_details that store item_id, quantity.The table currently only hold data for item_id, while the quantity has null value.Lets say the item id is 038
i created another table order_details2 as a temp table that have the same row which is item_id,quantity. This table contains data for both item_id, and quantity which is 038, and 3
the problem is, i dont know how to use data in order_details2.quantity which is 3 to insert it into order_details.quantity using the same references item_id
can any1 please show me, how am i doing this? im very new to programming..
If I understand correctly what you're after, then something like this should work:
UPDATE
(SELECT o1.item_id as id1, o1.quantity as qty1, o2.item_id as id2, o2.quantity as qty2
FROM order_details o1
JOIN order_details2 o2 on o1.item_id = o2.item_id) t
SET t.qty1 = t.qty2
You have to be careful about duplicate item_id values in any of the tables though.
Related
just like the caption, how do i fetch information from foreign key instead of id?
Here are the first table:
and here are the second table:
i want to display product_name and unit_price instead of id, how to do it?
You need to use join
Your query should look something like this:
SELECT ft.id, pr.product_name, pr.unit_price from firsttable as ft join product as pr on ft.product_id = pr.product_id;
Where firsttable is the table of the first screenshot and product is the table from the second screenshot.
I don't know what you need from the first table so I just took a column id, however I do not know if you have this column. Here you just put whatever you need from the first table.
I am newbie to sql advance and i am trying to insert rows to one table by selecting from another one. But some bug is coming out.
Here is my problem
So i have two tables
table1 having id(autoincrement), names
table2 having id(autoincrement), names
Now at starting table1 is empty and table2 having 2 rows
1,'myself'
2,'yourself'
So the problem starts here
When i execute following query
Insert into table1 (names) select (names) from table2
So now both rows of table2 must be copied to table1
Ya its working fine.
But what about autoincrement value of id?
By till now table1 autoincrement id should store 3 since next row to be inserted should have id 3
But its not working like expected so table1 autoincrement id stores 4 i.e, 1(current id value)+2*(no of rows inserted)-1
So next time when i execute same query it inserts row with id 4. Skips id=3.
This is problem hope you all got what i am talking about.
Thanks for helping in advance.
Try this
INSERT INTO TABLE1 SELECT * FROM TABLE2
Go ahead an copy your data over, and then you can modify the starting auto_inc to whatever you choose.
ALTER TABLE table AUTO_INCREMENT = 1000;
Alter Table will take around 1 sec as it first creates shadow of the table then the query is processed and if the no. of rows of table goes above then it may take around 20-30 sec. so I dont think that alter is good answer for this problem. Hope someone will help you with insert command such as:
Insert into table1 set id = 1; etc...
I've a table with the purpose of linking two other tables with foreign keys (table 3).
table1 = id, name
table2 = id, sport
table3 = fk_table1_id, dk_table2_id
I do server side code with php and i need a way to automatic insert a row into table 3 when a row is inserted into table2.
But i dont know how to do a php query that will get the generated id of the new row in table2?(The id of table one i do have stored in a php variable)Im a looking at a smart stored procedure?
Steps to follow:
Insert a row into table2.
Find the generated key from table2 and assign it to a PHP variable.
-- read this into a php variable, say, $last_key
SELECT LAST_INSERT_ID();
Use the $last_key in insert statement for table3.
You can Follow below Steps:
1) Make ID to AUTO_INCREMENT IN Table2
2) Create Store Procedure that work as below:
A) INSERT Value to your Table2 :
B) Increase ID value : SELECT LAST_INSERT_ID() INTO VARIABLE;
C) Insert Value to your Table3 : use VARIABLE to get ID of Table2
I have two tables one is called Players and the other is importdata. The importdata table consists of two fields the player id (PID) and the photo (Photo).
In the Players table I created a column for the Photo field to be imported into. What I would like to do is take the Photo field from the importdata table and insert it into the photo_high field in the Players table where the PID fields match.
I thought something like this would work, but it says that there is an unknown column.
INSERT INTO (`photo_high`)
SELECT PID, Photo
FROM importdata
WHERE Players.PID = importdata.PID
Can this be achieved with an SQL statement or do I have to write some kind of script? Any guidance would be great.
Players
PID
photo_high (empty)
importdata
PID
Photo (full of content)
I think you want update rather than insert:
update Players p join
ImportData id
on p.Pid = id.pid
set photo_high = id.photo;
insert creates new rows in a table. update changes values in existing fields.
I am trying to update ms sql server table's specific field data using webhook posted xml data
the below is php $tsql script
$tsql = "UPDATE Item SET Quantity = (Quantity - '$qty')
WHERE ItemLookupCode = '$sku'";
'$sku' is from xml data field which is webhook posted but the problem is for example i got 4 '$sku' from xml and 2 of '$sku' is matching with ItemLookupCode in 'Item' Table so i can update but the other 2'$sku' i have to find from another table using different field in another table - Item2(another table's name for example) so after i got 2 '$sku''s specific field in Item2 table i come bact to 'Item' table and i can update Quantity in Item table using different field using the data i got from Item2 table
i am sorry if i made u confused...
so basically what i am trying to do is if "$sku' from xml matches with ItemLookupCode in Item table then i update the quantity but if "$sku" from xml doen't matches or don't have in another word in Item table's ItemLookupCode then i how do i try to find some data from another table using those '$sku'
(ex) if some item's sku is not in ItemLookupCode column in Item table then use that "$sku" to select some column's specific values in Item2 table to update Item table's Quantity
Thank you so much guys!
UPDATE Item SET Quantity = (Quantity - '$qty')
From OtherTable o
WHERE Item.Name=o.Name and o.ItemLookupCode = '$sku'