Solved

Insert with SET how to handle join

Posted on 2016-11-25
6
84 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 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41901552
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
ID: 41901694
Its part of an trigger, thats why a "new" is involved.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41901700
Great  , you like the solution..?
1
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 41901747
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
ID: 41902087
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 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 41902289
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

696 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