LoginSignup
0
0

mysql2/promise でGROUP BYが使えない

Last updated at Posted at 2023-08-28

普通のSELECT文は取得できるけど、GROUP BYを使うと取得できなかったのでメモ。

×取得できないケース

const sql = 'SELECT user_id, COUNT(*) AS cnt FROM table WHERE user_id IN (?) GROUP BY user_id;';
const [rows] = await connection.execute<mysql.RowDataPacket[]>(sql, [userIds]);

〇取得できるケース

const sql = 'SELECT user_id, COUNT(*) AS cnt FROM table WHERE user_id IN (?) GROUP BY user_id;';
const [rows] = await connection.query<mysql.RowDataPacket[]>(sql, [userIds]);

connection.execute()だと複雑なSQLは取得できないっぽいです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0