?
Solved

Insert with SET how to handle join

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

Expert Comment

by:Pawan Kumar
ID: 41901700
Great  , you like the solution..?
1
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

764 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