Solved

Insert with SET how to handle join

Posted on 2016-11-25
6
91 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 29

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 29

Expert Comment

by:Pawan Kumar
ID: 41901700
Great  , you like the solution..?
1
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

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. …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…

691 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