Rumah > Soal Jawab > teks badan
Saya cuba menolak hasil pertanyaan SQL ke tatasusunan. Namun, ia nampaknya tidak berkesan. Saya tidak dapat mencari penyelesaian dalam talian. Jika sesiapa boleh membantu saya menyelesaikan masalah ini, saya amat berterima kasih.
let data = []; connection.query("SELECT DISTINCT referer FROM publisher", (error, results) => { if (error) { console.log(error); } else { results.forEach(async (result) => { let referer = result.referer; let today = new Date(); for (let i = 0; i < 24; i++) { let hour = i; let start = new Date(today); start.setHours(hour, 0, 0, 0); let end = new Date(today); end.setHours(hour, 59, 59, 999); connection.query("SELECT COUNT(*) AS count FROM publisher WHERE referer = ? AND first_seen >= ? AND first_seen <= ?", [referer, start, end], (error, results) => { if (error) { console.log(error); } else { let count = results[0].count; data.push({ referer: referer, hour: hour, count: count }); } }); } }); } });
P粉0468781972024-04-07 12:25:01
Anda boleh cuba mendapatkan data daripada MySql DB menggunakan pertanyaan tunggal
select referer, hour(first_seen) h, count(*) n from publisher where date(first_seen) = curdate() group by referer, hour(first_seen) order by referer, hour(first_seen)
Ini menghapuskan kaedah async dalaman dalam kod js.