Solved

Insert with SET how to handle join

Posted on 2016-11-25
6
26 Views
Last Modified: 2016-11-29
I have some MYSQL code that looks like this:

 
insert into TableA(
       `col1`,
      `col2`,
       `col3`
      )
      SELECT
         b.`row1`,
         b.`row2`,
         c.`row2`

  FROM table b AS b 
  LEFT JOIN tablec AS c ON c.`row1` = a.`row1`
  WHERE a.`row1` = new.`row1`;

Open in new window


Now i want to change this code into something like

insert  INTO TableA Set

`col1`= b.`row1`,
`col2` = b.`row2`, 
 `col3`=c.`row2`

  FROM table b AS b 
  LEFT JOIN tablec AS c ON c.`row1` = a.`row1`
  WHERE a.`row1` = new.`row1`;

Open in new window


But i cant get it to work. I do prefer to use SET because when i have many rows it get extremly confusing to track what value goes into what row without the set syntax.
0
Comment
Question by:Sunsales
  • 2
  • 2
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
Comment Utility
try..USE AS Keyword instead of  =   <<Note - Changed topics>>

insert  INTO TableA (`col1`,`col2`,`col3`)
SELECT  b.`row1` AS `col1` , b.`row2` AS `col2`, c.`row2` AS `col3`
FROM table b AS b 
LEFT JOIN tablec AS c ON c.`row1` = a.`row1`
WHERE a.`row1` = new.`row1`;

Open in new window


What is this new.`row1` ?
0
 

Author Comment

by:Sunsales
Comment Utility
Its part of an trigger, thats why a "new" is involved.
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Great  , you like the solution..?
1
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
Comment Utility
Hi!

The INSERT - SET syntax can't be used with the INSERT-SELECT syntax in an INSERT statement.
http://dev.mysql.com/doc/refman/5.7/en/insert.html

When you say "it get extremly confusing to track what value goes into what row without the set syntax", are you meaning the resultset of the select query or the complexity of the insert-statement ?

Regards,
    Tomas Helgi
0
 

Author Comment

by:Sunsales
Comment Utility
Pawan, now i understand Your solution, yesterday was late in the day and I did not ;) Yes it does solve the problem. Still i a lot of unnecessary code that make debugging and editing unnecessary difficult and the code obfuscated. Not exactly an elegant language.

Tomas Helgi, i refer to the complexity of the insert-statement. When there  is a complex insert statement, maybe 20 or more columns it gets hard to track an error down and to write it. I can solve it in code on the client side (Delphi), but then i will  loose performance and get at less solid solution.
0
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
Comment Utility
HI!

You say that you loose performance. And if this code is in a trigger then I would look at the tables in your query's join and
make sure that those tables in the join has appropriate indexes. Indexes that has the same columns as the columns used in the join and where clause. Also make sure that you optimize and analyze the tables on regular basis to maximize performance of queries and inserts/update and delete statements. That would solve your performance problems in some way.
The other thing to look at is how your insert/update and delete statements on the table that the trigger is bound to are done. Do you use batch statements ? If not I suggest you look at that to see if that helps. Table partitioning is also a good choice for very large tables.

Regards,
     Tomas Helgi
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now