Databases :: MySql :: INSERT...SELECT from multiple tables |
|||
| By: javaQQ |
Date: 18/06/2003 00:00:00 |
Points: 100 | Status: Answered Quality : Excellent |
|
Is the following code valid for MySQL? INSERT INTO Summary( column1, column2, column3, column4 ) SELECT a.one_field, a.another_field, b.field_one, c.field_two FROM Table_One AS a, Table_Two AS b, Table_Three AS c WHERE Table_One.common = Table_Two.common AND Table_Two.common = Table_Three.common AND Table_Three.common = Table_One.common Please critique. Many thanks in advance |
|||
| By: VGR | Date: 18/06/2003 08:04:00 | Type : Comment |
|
| yes, I think so. It even looks pretty very good :D |
|||
| By: TroyK | Date: 18/06/2003 09:04:00 | Type : Answer |
|
| javaQQ; How about using your table aliases in your JOINs... INSERT INTO Summary( column1, column2, column3, column4 ) SELECT a.one_field, a.another_field, b.field_one, c.field_two FROM Table_One AS a, Table_Two AS b, Table_Three AS c WHERE a.common = b.common AND b.common = c.common AND c.common = a.common --I think this AND condition is unneccessary Makes it a bit more readable. VGR; Point of curiosity... does MySQL support ANSI join syntax? e.g.: INSERT INTO Summary( column1, column2, column3, column4 ) SELECT a.one_field, a.another_field, b.field_one, c.field_two FROM Table_One AS a INNER JOIN Table_Two AS b ON a.common = b.common INNER JOIN Table_Three AS c ON b.common = c.common If so, javaQQ, you may want to get in the habit of using the ANSI JOIN syntax as the differences become important when doing OUTER JOINs (but not in your sample) HTH, TroyK, MCSD |
|||
| By: JakobA | Date: 18/06/2003 11:05:00 | Type : Comment |
|
| Yes you can do those inner joins in mysql <A HREF="http://www.mysql.com/doc/en/JOIN.html">http://www.mysql.com/doc/en/JOIN.html</a> |
|||
| By: VGR | Date: 18/06/2003 14:34:00 | Type : Assist |
|
| yes, but it's not necessary. mysql is more ansi-compliant than m$-sql server, the proprietary thingy from merdu$oft |
|||
| By: psadac | Date: 19/06/2003 03:26:00 | Type : Assist |
|
| one more : INSERT INTO Summary SELECT a.one_field AS column1, a.another_field AS column2, b.field_one AS column3, c.field_two AS column4 FROM Table_One a INNER JOIN Table_Two b USING(common) INNER JOIN Table_Three c USING(common) |
|||
| By: javaQQ | Date: 19/06/2003 08:46:00 | Type : Comment |
|
| Two points: ONE: >How about using your table aliases in your JOINs... >... >WHERE a.common = b.common >AND b.common = c.common >AND c.common = a.common --I think this AND condition is unneccessary Are you sure that that is allowed? According to the MySQL Pocket Reference (O'Reilly), p. 57, "You cannot use aliases in WHERE clauses as their values cannot be calculated at that point" TWO: Regarding the points: My impulse is to just split them four ways. Are there any objections to that? Many thanks for the responses so far. |
|||
| By: TroyK | Date: 19/06/2003 09:49:00 | Type : Comment |
|
| javaQQ; >>"You cannot use aliases in WHERE clauses as their values cannot be calculated at that point" I think this is refering to aliased _column_ names. e.g., you can't do this: SELECT col1 AS MyColumnName FROM MyTable WHERE MyColumnName = 1 I could be wrong, though, as my expertise lies more with MS SQL Server (T-SQL). I'd say give it a try. Re: the points, IMO, you should split them among any of the participants who were able to help. TroyK, MCSD |
|||
| By: JakobA | Date: 20/06/2003 05:04:00 | Type : Assist |
|
| Table aliases are legal anywhere in the sql command Fied-value aliases are legal only after the group by clause (before that thay may not have a meaning as in COUNT(field) AS nr, ) |
|||
| By: JakobA | Date: 20/06/2003 05:15:00 | Type : Comment |
|
| According to the mysql documentation you can even use field value aliases IN the group by clause. <A HREF="http://www.mysql.com/doc/en/Problems_with_alias.html">http://www.mysql.com/doc/en/Problems_with_alias.html</a> I think that would fail where the field is an accumulated value depending on grouping ( COUNT, SUM, MIN, ...) When it is not the alias is at least possible there. regards JakobA |
|||
| By: javaQQ | Date: 20/06/2003 05:32:00 | Type : Comment |
|
| Thanks for all the responses. I have increased the points to 100 and split them four ways. Many thanks again. |
|||
|
Do register to be able to answer |
|||
©2012 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








