CherryPAVoice
CherryPAVoice

Reputation: 43

How do I grab a MySQL column's position?

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?

Upvotes: 1

Views: 66

Answers (1)

Ankur Sharma
Ankur Sharma

Reputation: 96

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


    

Upvotes: 1

Related Questions