Click here to Skip to main content
15,883,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables "OpenOrder" and "OpenOrderTemp". I want look the difference and result insert in "OpenOrder", so I tried with this code but it doesn't work.

SQL
INSERT INTO  OpenOrder (orderId) 
SELECT OpenOrder.orderId 
FROM OpenOrder 
INNER JOIN OpenOrderTemp ON OpenOrder.* = OpenOrderTemp.* 
SET   OpenOrder.* = OpenOrderTemp.*
WHERE OpenOrder.* <> OpenOrderTemp.*;


example data OpenOrder
|symbol|orderid|clientorderid|price|etc....|
-------------------------------------------
|MDTUSD|1223156|web_121354542|42.00|
|REBUSD|1224578|web_128875666|22.00|
....
example data OpenOrderTemp
|symbol|orderid|clientorderid|price|etc.. |
------------------------------------------
|MDTUSD|1223156|web_121354542|42.00|
|REBUSD|1224578|web_128875666|22.00|
|ORNBTC|2585582|web_128545456|02.00|
....

I want to update OpenOrder with only this |ORNBTC|2585582|web_128545456|02.00|


I want to insert all fields.

What I have tried:

i tried this but unlucky not work

SQL
INSERT INTO  OpenOrder (orderId) 
SELECT OpenOrder.orderId 
FROM OpenOrder 
INNER JOIN OpenOrderTemp ON OpenOrder.* = OpenOrderTemp.* 
SET   OpenOrder.* = OpenOrderTemp.*
WHERE OpenOrder.* <> OpenOrderTemp.*;
Posted
Updated 4-Jul-22 0:51am
v2

1 solution

It's not so much it doesn't work as it won't compile. That is completely the wrong syntax - see MySQL INSERT INTO SELECT Explained By Practical Examples[^]
Drop the SET OpenOrder.* = OpenOrderTemp.* and list all of the columns instead of SELECT OpenOrder.orderId
You will also need to fix the ON clause for the join and list only the columns that link the tables - see MySQL :: MySQL 8.0 Reference Manual :: 13.2.10.2 JOIN Clause[^]

Edit: I took another look and there are other issues too. Try something like this:
SQL
INSERT INTO  OpenOrder (symbol,orderid,clientorderid,price)
SELECT ot.symbol,ot.orderid,ot.clientorderid,ot.price
FROM OpenOrderTemp ot
LEFT OUTER JOIN OpenOrder o ON o.symbol=ot.symbol 
							and o.clientorderid = ot.clientorderid 
							and o.orderid = ot.orderid
							and o.price = ot.price
WHERE o.symbol is null
Points to note:
- I have listed all of the columns I intend to insert and I explicitly name the columns in the correct order in the SELECT part of the query.
- I have used Table aliases (o, ot) to save having to type out the full table name each time.
- I've used a LEFT OUTER join and tested for NULL values as we're trying to find values that are not already in OpenOrder, not the stuff that matches - you might find this article helps Visual Representation of SQL Joins[^]
- You don't want to insert values from OpenOrder - they will null, it's the values in OpenOrderTemp that you need
- I've used all of the columns in the join but that is unusual - normally you would have a specific key that would link the tables - in this case probably just orderid
 
Share this answer
 
v2
Comments
[no name] 4-Jul-22 6:51am    
but is not possible to do without explicitly name the columns ?? because i use all column and is so much a column o_O
thanks
CHill60 4-Jul-22 8:45am    
Yes, strictly speaking it is possible but it is not good practice (and many will think it is lazy). Using wildcards like * leaves you vulnerable to changes in the table schema and assumes that exactly the same number of columns in the same order are on both tables. It is better to be completely in control of your own code.
It is easy enough to generate a list of columns on a table to paste into your code e.g. MySQL get column names – thisPointer[^]
[no name] 4-Jul-22 8:51am    
but i think not have this problem for consinstency because when program run clone a first OpenOrder in OpenOrderTemp and after do som insert in OpenOrderTemp and after i want compare 2 table and after drop OpenOrderTemp
CHill60 4-Jul-22 9:57am    
That's up to you, I can only advise you on best practice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900