Jump to content
  • 0

(SQL_QUERY) Update existing data


j2rhyme

Question


  • Group:  Members
  • Topic Count:  6
  • Topics Per Day:  0.00
  • Content Count:  17
  • Reputation:   0
  • Joined:  06/20/15
  • Last Seen:  

Hi, I would like to know how to update an existing data in sql_query.

I made a Donation NPC with a Donor Ranking. I've somehow made it work but when I donate twice. I want to add my last donation and my current donation in the ranking. Instead of creating a new one.

And also, I would like to know how ORDER BY works. I want it ordered by ascending, but clearly I made some mistake because it did not work.

Here is my code:

case 2:
    mes "test";
    query_logsql "SELECT `name`, `donateamount` FROM `donornames` ORDER BY `donornames`.`donateamount`", .@donor_name$, .@donate_amount;
    for (set .@i, 0; .@i < getarraysize(.@donate_amount); set .@i, .@i+1){
        mes "^296900["+.@i+"]^000000 - " + "^ff4d00" + .@donor_name$[.@i] + "^000000 has bought " + .@donate_amount[.@i] + " donation tickets.";
}//endfor
close;
 
INGAME SCREENSHOT:

Untitled.jpg

 
As you can see here, TEST has donated twice, I want it to show 700 instead of 2 separate lines of 200 and 500. Also I would like it to be in ascending order; the highest donors at the top, while the lowest at the bottom. And I'm sure it's possible with ORDER BY but I'm sure I used it incorrectly. Same with UPDATE.
Edited by j2rhyme
Link to comment
Share on other sites

2 answers to this question

Recommended Posts


  • Group:  Members
  • Topic Count:  31
  • Topics Per Day:  0.01
  • Content Count:  283
  • Reputation:   31
  • Joined:  07/08/14
  • Last Seen:  

Do you want the donations to be displayed with an alphabetical order or the highest/lowest donation first?

nevermind didn't see what you wrote at the bottom..

 

Alphabetical order:

query_logsql "SELECT `name`, `donateamount` FROM `donornames` ORDER BY `name`", .@donor_name$, .@donate_amount;

Lowest first:

query_logsql "SELECT `name`, `donateamount` FROM `donornames` ORDER BY `donateamount`", .@donor_name$, .@donate_amount;

Highest first:

query_logsql "SELECT `name`, `donateamount` FROM `donornames` ORDER BY `donateamount` DESC", .@donor_name$, .@donate_amount;

For the update thing when you want to save the donations do something like this

.@n = query_logsql ("SELECT `donateamount` FROM `donornames` WHERE `<your primary key>` = '<value to compare>'",.@donate_amount);

if (.@n == -1) query_logsql ("INSERT INTO `donornames` (...) VALUES (...)");

else query_logsql ("UPDATE `donornames` SET `donateamount` = '"+(.@donate_amount+<amount of points to add>)+"' WHERE `<your primary key>` = '<value to compare>'");
Edited by Kurofly
  • Upvote 1
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  6
  • Topics Per Day:  0.00
  • Content Count:  17
  • Reputation:   0
  • Joined:  06/20/15
  • Last Seen:  

Thank you kurofly! It works perfectly fine!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...