0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

クエリだけでCSVのヘッダーを作る

Last updated at Posted at 2024-07-24

よくあるやり方

SELECT
 name,
 email,
 created_at
FROM
 user;

取得後にリストでヘッダーを作成

$header = ['名前', 'メールアドレス', '作成日時']; 
$data = $this->dataGet()->toArray(); 
$csvData = array_merge([$header], $data); 

クエリのみでヘッダーを追加する方法

SELECT
 '名前' as name,
 'メールアドレス' as email,
 '作成日時' as created_at,
 0 as sort

UNION ALL

SELECT
 name,
 email,
 created_at,
 1 as sort
FROM
 user
ORDER BY sort;

クエリのみで完結させたい場合や、csv用のviewなどを使用する際に便利そうです

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?