473,408 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to update the right column?

2
hye!

Im a newbie in MySQL and Php, i got a problem to update column in db. Here is the issue:

i have 2 tables named 'apply' and 'balance'.

Expand|Select|Wrap|Line Numbers
  1. Table: apply
  2. userId  |  leaveType    |daysApply
  3. 1       |     Annual    |         3
  4. 2       |    Medical    |        1
  5.  
  6. Table: balance
  7. userId  |  Annual   | Medical
  8. 1       |     14       |     3
  9. 2       |    14       |    3

When user apply for leave, the system will deduct the value from table:balance based on apply.leaveType = balance.@column name. My problem is i dont know how to update the column value based on the apply.leaveType value from other table.

i try to do this but it doesnt make sense:

Expand|Select|Wrap|Line Numbers
  1. UPDATE  `balance`, `apply` 
  2. SET     `balance.Annual`=`balance.Annual`-`apply.daysApply`, 
  3.         `balance.Medical`=`balance.Medical`-`apply.daysApply` 
  4. WHERE   `balance.Annual`=`apply.leaveType` 
  5. AND     `balance.Medical`=`apply.leaveType` 
  6. AND     `balance.userId`=`apply.userId`
Hope someone can help me. Any helps are appreciated.
Jan 27 '10 #1
3 1795
code green
1,726 Expert 1GB
I set out writing an UPDATE JOIN statement before I realised your database design seems to be wrong
The balance table should have multiple entries for each user.
ie one medical and one annual
Expand|Select|Wrap|Line Numbers
  1. Table: balance
  2. userId  leavetype         days
  3. 1          Annual             14           
  4. 1          Medical             3
  5. 2          Annual             14           
  6. 2          Medical             3
Taking this further for better performance and maintenance,
I would have a third table of leave types
Expand|Select|Wrap|Line Numbers
  1. Table: type_codes
  2. code   leavetype         
  3. 1          Annual                       
  4. 2         Medical       
and balance table
Expand|Select|Wrap|Line Numbers
  1. Table: balance
  2. userId  leavetype         days
  3. 1          1                    14           
  4. 1          2                     3
  5. 2          1                    14           
  6. 2          2                     3
Now the query should almost write itself
Jan 27 '10 #2
Atli
5,058 Expert 4TB
Hey.

It occurs to me, that the balance table in the original post doesn't need to be a real table. You cold create a View for it.

That is; the columns of the balance table could be generated by doing a SELECT on the apply table. Thus, the balance table is redundant. A SELECT query - or a View - could take it's place.

For example, if we use code_green's balance table - which is, by the way, named incorrectly; it takes the place of the apply table, not the balance table - as the apply table, we could create the following View:
Expand|Select|Wrap|Line Numbers
  1. CREATE VIEW `balance` AS
  2. SELECT
  3.     o.`userID`,
  4.     (   SELECT  SUM(i.`days`)
  5.         FROM    `apply` AS i
  6.         WHERE   i.`userID` = o.`userID`
  7.         AND     i.`leavetype` = 1
  8.     ) AS 'Annual',
  9.     (   SELECT  SUM(i.`days`)
  10.         FROM    `apply` AS i
  11.         WHERE   i.`userID` = o.`userID`
  12.         AND     i.`leavetype` = 2
  13.     ) AS 'Medical'
  14. FROM    `apply` AS o
  15. GROUP BY o.`userID`
Which would imitate a balance table, allowing you to simply call it as a normal table:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM `balance`;
Expand|Select|Wrap|Line Numbers
  1. +--------+--------+---------+
  2. | userID | Annual | Medical |
  3. +--------+--------+---------+
  4. |      1 |      3 |      16 | 
  5. |      2 |      7 |       9 | 
  6. +--------+--------+---------+

Also, just to point this out.
In your query you did this:
Expand|Select|Wrap|Line Numbers
  1. WHERE `balance.Annual`=`apply.leaveType` 
You can not group table and column names together inside a single back-tic (`) group. That should have looked like this:
Expand|Select|Wrap|Line Numbers
  1. WHERE `balance`.`Annual`=`apply`.`leaveType` 
The back-tics are meant to encapsulate a single database, table or column name, not a list of names.
Jan 27 '10 #3
syafia
2
ok..i got the solution at last. Thanks to code green for realizing me that something going wrong with my db table. I've alter back my db table and got the solution to update the balance table.

And thanks to atli for giving me new knowledge about using VIEW in Mysql.

Both helps are really appreaciated ^_^
Jan 28 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Raja | last post by:
I have a lookup table that holds two columns - a key value and another column containing items. For e.g. Col1 Col2 1 10 1 15 1 12 2 20 2 15 ...
8
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
17
by: Benoit Martin | last post by:
I'm working on a project in VB.net connecting to a SQL Server 2000 database that I can't modify I created a dataset with a schema identical to the DB. When trying to update the DB from the dataset...
4
by: Jeremy | last post by:
My app is throwing an exception regarding trying to post a null to a required field. Examining the datarow in question, I see a valid date value. The row's state is "added" I'm relying on an ...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
0
by: JayD | last post by:
I am using Visual Web Developer (vb.net) and have a gridview in a form. I can update records using standard EDIT/UPDATE buttons. However, there are two fields in every record (called ModifiedBy and...
6
by: ssailor | last post by:
Hi, everybody, I am rusty with VB. In the following code, ------------------------------------------------------ Dim dbCmd1 As OleDbCommand = New OleDbCommand("UPDATE tblLogin SET Password =...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
2
by: BobLewiston | last post by:
Some of you may have seen my earlier thread “PasswordHash NULL problem”. I’ve started a new thread because investigation has shown that the problem is actually quite different than I previously...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.