1

I'm currently working on creating a Discord bot with a level module, and I can't seem to grab the position of a MySQL column. This is to display the current rank of the user. Here's the query:

connection.query(`SELECT * FROM levels WHERE GuildID = ${message.guild.id} ORDER BY UserLvl DESC, LevelExp DESC`, function(error6, results6) {
  result_json6 = JSON.stringify(results6);
  final_result6 = JSON.parse(result_json6);
});

I've tried several things, from forEach to while to for and more. Is there any way to grab the position of a column and display it?

  • Does this https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call answer your question? – Sergiu Paraschiv Oct 30 '20 at 15:10
  • Columns don't have a "position" in a SQL store. Please define what you mean by "column's position". – Randy Casburn Oct 30 '20 at 15:10
  • @SergiuParaschiv - the OP clearly has a callback function coded. It is not a dupe of that question. – Randy Casburn Oct 30 '20 at 15:12
  • @RandyCasburn I'm 90% sure he's accessing `result_json6` _outside_ the callback, but might be wrong. Hence the question. – Sergiu Paraschiv Oct 30 '20 at 15:13
  • @RandyCasburn I'm looking to show how high the user ranks in the server regarding levels. But for that, I need to look how much EXP they have and what level they are. But I don't know where to start either. The command works but I simply can't get the ranking number to work. – CherryPAVoice Oct 30 '20 at 15:14
  • @CherryPAVoice Please show us _how_ you are trying to access `result_json6`. We need to see more code to help. – Sergiu Paraschiv Oct 30 '20 at 15:14
  • @SergiuParaschiv No, I didn't add a var or const value there for some dumb reason. I'm not accessing any variable displayed here from outside the callback. – CherryPAVoice Oct 30 '20 at 15:15
  • Got it. Can you then show us an example of a `results6` please? – Sergiu Paraschiv Oct 30 '20 at 15:16
  • I'll log it for you, give me a minute! – CherryPAVoice Oct 30 '20 at 15:17
  • @SergiuParaschiv - sure that assumption makes sense here. I just figured OP was giving us some insight. – Randy Casburn Oct 30 '20 at 15:17
  • 2
    Does this answer your question? [How to add rank column?](https://stackoverflow.com/questions/39388624/how-to-add-rank-column) – Thomas Sablik Oct 30 '20 at 15:17
  • I'm receiving all current data in an array format with RowDataPackets everywhere. For example: RowDataPacket { id: 17, GuildID: 'GUILD_ID', UserID: 'USER_ID', TotalExp: 5, LevelExp: 5, ExpNeeded: 300, UserLvl: 0 } – CherryPAVoice Oct 30 '20 at 15:18
  • https://www.mysqltutorial.org/mysql-window-functions/mysql-rank-function/ or https://www.mysqltutorial.org/mysql-window-functions/mysql-row_number-function/ – Thomas Sablik Oct 30 '20 at 15:18
  • 1
    @CherryPAVoice - your question has to do with how to structure your database and make a specific SQL statement. Thomas' comment is probably the answer. – Randy Casburn Oct 30 '20 at 15:19

1 Answers1

1

Define column order in the query. For eg. SELECT Column1, Column2, Column3... FROM TABLE

Then retrieve column with their name and row position 0..N-1 (N = size of result6)

  • If you want to get Column2 from first row, then result6[0].Column2
  • If you want to get Column3 from second row, then result6[1].Column3

You can put row position using for loop, lets say i = 0 to N-1, and get the value as

result6[i].Column1