[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Insert with SET how to handle join

Posted on 2016-11-25
6
Medium Priority
?
103 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 32

Accepted Solution

by:
Pawan Kumar earned 2000 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 32

Expert Comment

by:Pawan Kumar
ID: 41901700
Great  , you like the solution..?
1
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

649 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