Recent

Author Topic: [SOLVED] how subtract 2 from all the grades?  (Read 12657 times)

Delphilearner

  • New Member
  • *
  • Posts: 26
[SOLVED] how subtract 2 from all the grades?
« on: February 28, 2016, 11:34:55 pm »
hello

i have 2 tables

Table1

ID
Name
BirthDay

Table2

Math
philosophy
ID

what I want to do is how can i subtract 2 from Table2 (field maht)  for all record groub by ID

and I want to run this code when the program start
i put it in FormShow or FormCreate
« Last Edit: March 07, 2016, 03:35:29 pm by Delphilearner »

balazsszekely

  • Guest
Re: how subtract 2 from all the grades?
« Reply #1 on: February 29, 2016, 07:59:07 am »
Hi Delphilearner,

It's absolutely not clear what are you after(at least for me). Can you be more specific?

regards,
GetMem

Delphilearner

  • New Member
  • *
  • Posts: 26
Re: how subtract 2 from all the grades?
« Reply #2 on: February 29, 2016, 06:55:33 pm »
thank you for your Reply

what i want to do is subtract 2 form all the fields MAtH in the TABLE2 (which contains students Grades)

for examble

15-2=13
10-2=8
...


I have 2 Tables

TABLE1 contains informations about all the students (name, Birthday ...)
TABLE2 Contains their marks(math,philosophy....)

balazsszekely

  • Guest
Re: how subtract 2 from all the grades?
« Reply #3 on: February 29, 2016, 08:35:43 pm »
Do you wish to save back the subtracted value or only display it?

Code: Pascal  [Select][+][-]
  1. select (T2.Math - 2) as Math_Sub from Table2 T2
  2. where T2.Math is not null /*add other filter here*/

PS: Still not clear what is the link between table1 and table2? Do you have a foreign key?  I would use a Student_ID in table2 which points to ID in Table1
« Last Edit: February 29, 2016, 08:40:00 pm by GetMem »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: how subtract 2 from all the grades?
« Reply #4 on: February 29, 2016, 08:44:44 pm »
PS: Still not clear what is the link between table1 and table2?
In red: grouped by ID. That's the link  ;)

balazsszekely

  • Guest
Re: how subtract 2 from all the grades?
« Reply #5 on: February 29, 2016, 08:49:05 pm »
Quote
@molly
In red: grouped by ID. That's the link  ;)

Thanks @molly! Feel free to help the OP with further guidance.  Apparently you did understand better the requirement then I did. :)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: how subtract 2 from all the grades?
« Reply #6 on: February 29, 2016, 08:57:29 pm »
@getmem:

merely quoting OT:
...
what I want to do is how can i subtract 2 from Table2 (field maht)  for all record groub by ID

Unfortunately i don't have a clue about SQL  :-[

So yes, i've bitten my own arse here but assumed you'd overlooked it just as i initially did. I apologize if i overstepped.

balazsszekely

  • Guest
Re: how subtract 2 from all the grades?
« Reply #7 on: February 29, 2016, 09:03:20 pm »
Quote
So yes, i've bitten my own arse here but assumed you'd overlooked it just as i initially did. I apologize if i overstepped.
You don't have to apologize, in fact you're right. I did miss the red part somehow, probably because ID is always a unique identifier, never a link to another table, at least this is how it should be.

PS: By the way, it's very hard to help somebody when the requirements are not clear. 

Code: Pascal  [Select][+][-]
  1. select T1.ID, T1.Name, T1.BirthDay, (T2.Math - 2) as Math_Sub
  2. from Table1 T1
  3. left join Table2 T2 on T2.ID = T1.ID
« Last Edit: February 29, 2016, 09:17:53 pm by GetMem »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: how subtract 2 from all the grades?
« Reply #8 on: February 29, 2016, 09:43:06 pm »
@GetMem
Quote
PS: By the way, it's very hard to help somebody when the requirements are not clear
Yups, unfortunately i feel that way sometimes as well :-S

Thanks for the solution: new topic required research, and i was attempting to solve it with a clumsy where t1.id = t2.id statement, trying to fit in the subtraction in the select clause %)

(i believe i found the (forgotten) reason why i never got the hang of things regarding databases  :D )

Never figured it was possible to do such subtraction in a where clause, so thank you for having taught me something new.

balazsszekely

  • Guest
Re: how subtract 2 from all the grades?
« Reply #9 on: February 29, 2016, 09:49:05 pm »
@molly
I'm under the impression that the OP wants to save back the subtracted value to the database. If this is the case we need a completely different approach, like a loop with a query combined with an update script.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: how subtract 2 from all the grades?
« Reply #10 on: February 29, 2016, 10:07:55 pm »
@GetMem:
Yes, you are correct. If indeed that is what OP had envisioned.

But, pardon my LOL as i quote another requirement OP mentioned:
..and I want to run this code when the program start i put it in FormShow or FormCreate
Which, for sure will make everyone flunk in the end :D

But apparently, until made clear, we can do as seen fit. In which case i would opt for filling math field with zero's  >:D

balazsszekely

  • Guest
Re: how subtract 2 from all the grades?
« Reply #11 on: February 29, 2016, 10:13:01 pm »
Quote
Which, for sure will make everyone flunk in the end :D
Yeah  :D! I think we better wait for more info.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: how subtract 2 from all the grades?
« Reply #12 on: March 01, 2016, 03:38:09 pm »
@molly
I'm under the impression that the OP wants to save back the subtracted value to the database. If this is the case we need a completely different approach, like a loop with a query combined with an update script.
Just for my own clarification/understanding: could something like that also be achieved by using a sql update statement ?

The documentation on that command seems to suggest it is capable of doing so, but might perhaps not work in conjunction with 'syncing' both tables (i have yet to figure out/understand that part).

Scary stuff though, one mistake and you're screwed  :D
(for the casual reader: always backup before experimenting)

balazsszekely

  • Guest
Re: how subtract 2 from all the grades?
« Reply #13 on: March 01, 2016, 04:13:00 pm »
I don't think it's possible to update with a single query, because first you have to select the Math field to subtract 2, then update each row accordingly.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: how subtract 2 from all the grades?
« Reply #14 on: March 01, 2016, 04:42:33 pm »
It should be possible to use SELECT as the table within UPDATE.

Like there: http://stackoverflow.com/questions/1262786/mysql-update-query-based-on-select-query
« Last Edit: March 01, 2016, 04:47:04 pm by User137 »

 

TinyPortal © 2005-2018