実現したいこと
idが6以上になった場合、idが若い順から5件削除し表示したい。
試したこと
CASE文でfoodテーブルにidが6以上のものがあったら、idを昇順にして
5件削除する、それ以外の場合は idを昇順に表示
app.js
1app.get('/', (request, response) => { 2 3 const sql="CASE WHEN SELECT * from food where id >= 6 THEN delete from food order by id limit 5 ELSE SELECT id gram from food order by id asc limit 5 END"; 4 con.query(sql, function (err, result, fields) { 5 if (err) throw err; 6 response.send(result) 7 }); 8});
エラー結果
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE WHEN SELECT * from food where id >= 6 THEN delete from food order by id lim' at line 1
文法にミスがあると出た、構文通りに書いたのですが
原因箇所がわからず苦戦しております。
使用した構文
CASE WHEN 条件 THEN 真の時に返す値 ELSE 偽の時に返す値 END
全体のapp.js
app.js
1const express = require('express') 2const app = express() 3const path = require('path') 4const bodyParser = require('body-parser') 5const port = 3000 6const mysql = require('mysql'); 7 8const con = mysql.createConnection({ 9 host: 'localhost', 10 user: 'root', 11 password: '*****', 12 database:'rate_db' 13}); 14 15app.get('/', (request, response) => { 16 const sql="CASE WHEN SELECT * from food where id >= 6 THEN delete from food order by id limit 5 ELSE SELECT id gram from food order by id asc limit 5 END"; 17 con.query(sql, function (err, result, fields) { 18 if (err) throw err; 19 response.send(result) 20 }); 21 }); 22 23// ご飯用 24app.get('/food-data', (req, res) => 25 res.sendFile(path.join(__dirname, 'html/form.html'))) 26app.use(bodyParser.urlencoded({ extended: true })); 27app.post('/food-data', (req, res) => { 28 const sql = "INSERT INTO food SET ?" 29 con.query(sql,req.body,function(err, result, fields){ 30 if (err) throw err; 31 console.log(result); 32 res.redirect('/'); 33 34 }); 35}); 36 37//トイレ用 38app.get('/toilet-data', (req, res) => 39 res.sendFile(path.join(__dirname, 'html/toilet-form.html'))) 40app.use(bodyParser.urlencoded({ extended: true })); 41app.post('/toilet-data', (req, res) => { 42 const sql = "INSERT INTO toilet SET ?" 43 con.query(sql,req.body,function(err, result, fields){ 44 if (err) throw err; 45 console.log(result); 46 res.redirect('/'); 47 48 }); 49}); 50// ブラッシング用 51app.get('/brushing-data', (req, res) => 52 res.sendFile(path.join(__dirname, 'html/brushing-form.html'))) 53app.use(bodyParser.urlencoded({ extended: true })); 54app.post('/brushing-data', (req, res) => { 55 const sql = "INSERT INTO brushing SET ?" 56 con.query(sql,req.body,function(err, result, fields){ 57 if (err) throw err; 58 console.log(result); 59 res.redirect('/'); 60 61 }); 62}); 63// 体重用 64app.get('/weight-data', (req, res) => 65 res.sendFile(path.join(__dirname, 'html/weight-form.html'))) 66app.use(bodyParser.urlencoded({ extended: true })); 67app.post('/weight-data', (req, res) => { 68 const sql = "INSERT INTO weight SET ?" 69 con.query(sql,req.body,function(err, result, fields){ 70 if (err) throw err; 71 console.log(result); 72 res.redirect('/'); 73 74 }); 75}); 76 77 78app.listen(port, () => console.log(`Example app listening on port ${port}!`))
どなたかお時間のある方、ご回答のほどお願い申し上げます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/03/24 10:53