【发布时间】:2021-07-14 05:13:18
【问题描述】:
我有一个只提取 1 个结果的代码,我需要它来提取所有结果(或将其限制为 10 个)。有人可以帮助我或指出正确的方向吗?
con.query(`SELECT * FROM events WHERE closed = 'No'`, (err, rows) => {
if (err) throw err;
let sql;
let eventID = rows[0].eventnumber;
let hostID = rows[0].host;
let description = rows[0].eventname;
let participants = rows[0].participants
const event = `Event ${eventID}: ${description} - Hosted by: <@` + hostID + `>. Participants: ${participants}`
const listEmbed = new Discord.MessageEmbed()
.setColor('#ff6600')
.setTitle('Events')
.setDescription(`${event}`)
message.channel.send(listEmbed);
con.query(sql);
});
来自数据库的信息如下:
| eventnumber | host | eventname | closed | participants |
|---|---|---|---|---|
| 16 | 123456789012345678 | test raid | Yes | test0 test1 |
| 17 | 123456789012345678 | test raid | No | test2 test3 test4 |
| 18 | 123456789012345678 | test raid | No | test5 test6 test7 |
| 19 | 123456789012345678 | test raid | Yes | test0 test1 |
| 20 | 123456789012345678 | test raid | No | test2 test3 test4 |
| 21 | 123456789012345678 | test raid | No | test5 test6 test7 |
| 22 | 123456789012345678 | test raid | Yes | test0 test1 |
| 23 | 123456789012345678 | test raid | No | test2 test3 test4 |
| 24 | 123456789012345678 | test raid | No | test5 test6 test7 |
| 25 | 123456789012345678 | test raid | Yes | test0 test1 |
| 26 | 123456789012345678 | test raid | No | test2 test3 test4 |
| 27 | 123456789012345678 | test raid | No | test5 test6 test7 |
编辑:
所以我找到了一种让它显示 10 个事件的方法,但它看起来很难看,我知道有更好的方法似乎无法解决。另外,如果没有 10 个结果满足条件,代码就会出错。
con.query(`SELECT * FROM events WHERE closed = 'No'`, (err, rows) => {
if (err) throw err;
let sql;
let firstID = rows[0].eventnumber;
let firstHost = rows[0].host;
let firstDescription = rows[0].eventname;
let firstParticipants = rows[0].participants;
let secondID = rows[1].eventnumber;
let secondHost = rows[1].host;
let secondDescription = rows[1].eventname;
let secondParticipants = rows[1].participants;
let thirdID = rows[2].eventnumber;
let thirdHost = rows[2].host;
let thirdDescription = rows[2].eventname;
let thirdParticipants = rows[2].participants;
let fourthID = rows[3].eventnumber;
let fourthHost = rows[3].host;
let fourthDescription = rows[3].eventname;
let fourthParticipants = rows[3].participants;
let fifthID = rows[4].eventnumber;
let fifthHost = rows[4].host;
let fifthDescription = rows[4].eventname;
let fifthParticipants = rows[4].participants;
let sixthID = rows[5].eventnumber;
let sixthHost = rows[5].host;
let sixthDescription = rows[5].eventname;
let sixthParticipants = rows[5].participants;
let seventhID = rows[6].eventnumber;
let seventhHost = rows[6].host;
let seventhDescription = rows[6].eventname;
let seventhParticipants = rows[6].participants;
let eigthID = rows[7].eventnumber;
let eigthHost = rows[7].host;
let eigthDescription = rows[7].eventname;
let eigthParticipants = rows[7].participants;
let ninethID = rows[8].eventnumber;
let ninethHost = rows[8].host;
let ninethDescription = rows[8].eventname;
let ninethParticipants = rows[8].participants;
let tenthID = rows[9].eventnumber;
let tenthHost = rows[9].host;
let tenthDescription = rows[9].eventname;
let tenthParticipants = rows[9].participants;
const firstEvent = `**Event ${firstID}:** ${firstDescription}\n**Hosted by:** <@` + firstHost + `>\n**Participants:** ${firstParticipants}`
const secondEvent = `**Event ${secondID}:** ${secondDescription}\n**Hosted by:** <@` + secondHost + `>\n**Participants:** ${secondParticipants}`
const thirdEvent = `**Event ${thirdID}:** ${thirdDescription}\n**Hosted by:** <@` + thirdHost + `>\n**Participants:** ${thirdParticipants}`
const fourthEvent = `**Event ${fourthID}:** ${fourthDescription}\n**Hosted by:** <@` + fourthHost + `>\n**Participants:** ${fourthParticipants}`
const fifthEvent = `**Event ${fifthID}:** ${fifthDescription}\n**Hosted by:** <@` + fifthHost + `>\n**Participants:** ${fifthParticipants}`
const sixthEvent = `**Event ${sixthID}:** ${sixthDescription}\n**Hosted by:** <@` + sixthHost + `>\n**Participants:** ${sixthParticipants}`
const seventhEvent = `**Event ${seventhID}:** ${seventhDescription}\n**Hosted by:** <@` + seventhHost + `>\n**Participants:** ${seventhParticipants}`
const eigthEvent = `**Event ${eigthID}:** ${eigthDescription}\n**Hosted by:** <@` + eigthHost + `>\n**Participants:** ${eigthParticipants}`
const ninethEvent = `**Event ${ninethID}:** ${ninethDescription}\n**Hosted by:** <@` + ninethHost + `>\n**Participants:** ${ninethParticipants}`
const tenthEvent = `**Event ${tenthID}:** ${tenthDescription}\n**Hosted by:** <@` + tenthHost + `>\n**Participants:** ${tenthParticipants}`
const listEmbed = new Discord.MessageEmbed()
.setColor('#ff6600')
.setTitle('Events')
.setDescription(`${firstEvent}\n\n${secondEvent}\n\n${thirdEvent}\n\n${fourthEvent}\n\n${fifthEvent}\n\n${sixthEvent}\n\n${seventhEvent}\n\n${eigthEvent}\n\n${ninethEvent}\n\n${tenthEvent}`)
message.channel.send(listEmbed);
con.query(sql);
});
非常感谢您的帮助。
谢谢。
【问题讨论】: