Comments on: How to avoid many-to-one problems in SQL http://www.xaprb.com/blog/2006/03/11/many-to-one-problems-in-sql/ Stay curious! Thu, 02 May 2013 12:36:53 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: ross http://www.xaprb.com/blog/2006/03/11/many-to-one-problems-in-sql/#comment-19664 ross Sat, 01 Oct 2011 03:04:49 +0000 http://www.xaprb.com/blog/?p=104#comment-19664 This post is so old no wonder he didn’t answer my question. Btw I still use updates with join, but NOT when I will get a value from tb2 grouped by a column from tb1.

for example in `Customers` table, I will update the column `TotalItemsObtained` every time the customer make a sales transaction, `Sales` table saves every transaction of a customer.

Customers Table Sales Table
CustomerID CustID
TotalItemsObtained QtyOfSales

Example,

Customer 001 bought 2 items today

INSERT INTO `Sales` VALUES(’001′, ’2′);

UPDATE `Customers` INNER JOIN `Sales` ON `CustomerID` = `CustID` SET `TotalItemsObtained` = `QtyOfSales`;

(this is fine as long as you are sure that’s the very first transaction of Customer 001)

Customer 001 bought another 4 items on the second day

INSERT INTO `Sales` VALUES(’001′, ’4′);

UPDATE `Customers` INNER JOIN `Sales` ON `CustomerID` = `CustID` SET `TotalItemsObtained` = `QtyOfSales`;

(this is where this blog enters, what `QtyOfSales` will you get? 2 or 4? so this blog suggests..)

UPDATE `Customers` INNER JOIN `Sales` ON `CustomerID` = `CustID` SET `TotalItemsObtained` = SUM(`QtyOfSales`);

]]>
By: alex http://www.xaprb.com/blog/2006/03/11/many-to-one-problems-in-sql/#comment-19663 alex Thu, 29 Sep 2011 14:35:03 +0000 http://www.xaprb.com/blog/?p=104#comment-19663 So basically, you are trying to say that Update with JOINs is non-standard (syntactically) and should be avoided ?

]]>
By: ross http://www.xaprb.com/blog/2006/03/11/many-to-one-problems-in-sql/#comment-19436 ross Fri, 17 Jun 2011 11:58:56 +0000 http://www.xaprb.com/blog/?p=104#comment-19436 how will I update multiple columns of tb1 from values of tb2 using the standard update query?

will it be like this?

UPDATE tb1 SET col1 = (SELECT col1 FROM tb2 WHERE tb1.id = tb2.id), col2 = (SELECT col2 FROM tb2 WHERE tb1.id = tb2.id);

Thanks

]]>
By: Re: bookmarks and keywords | Bla.es http://www.xaprb.com/blog/2006/03/11/many-to-one-problems-in-sql/#comment-13429 Re: bookmarks and keywords | Bla.es Mon, 01 Oct 2007 09:58:02 +0000 http://www.xaprb.com/blog/?p=104#comment-13429 [...] http://www.xaprb.com/blog/2006/03/11/many-to-one-problems-in-sql/ [...]

]]>
By: Xaprb http://www.xaprb.com/blog/2006/03/11/many-to-one-problems-in-sql/#comment-3006 Xaprb Sun, 07 Jan 2007 15:02:59 +0000 http://www.xaprb.com/blog/?p=104#comment-3006 I probably answered your question in one of my two posts on duplicate rows. Here is the first: how to find duplicate rows.

]]>