Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

แสดงผลแบบขยาย (Expanded Output) ใน MySQL คล้าย PostgreSQL

MySQL รองรับการแสดงผลแนวตั้ง (Vertical Output) ด้วยอักขระ \G แทน Semicolon (;) คล้าย \x ใน PostgreSQL

รูปแบบคำสั่ง

SELECT * FROM yourTableName\G

ตัวอย่าง

1. สร้างตารางและข้อมูล

mysql> create table expandedOutputDemo (
    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    -> EmployeeName varchar(20),
    -> EmployeeAge int
    -> );

mysql> insert into expandedOutputDemo(EmployeeName,EmployeeAge) values
    -> ('Larry',27),('Mike',29),('Sam',31),('Bob',24),('Carol',28);

2. แสดงผลปกติ (Table Format)

mysql> select * from expandedOutputDemo;
+------------+--------------+-------------+
| EmployeeId | EmployeeName | EmployeeAge |
+------------+--------------+-------------+
| 1          | Larry        | 27          |
| 2          | Mike         | 29          |
| 3          | Sam          | 31          |
| 4          | Bob          | 24          |
| 5          | Carol        | 28          |
+------------+--------------+-------------+

3. แสดงผลแบบขยาย (\G)

mysql> select * from expandedOutputDemo\G
*************************** 1. row ***************************
EmployeeId: 1
EmployeeName: Larry
EmployeeAge: 27
*************************** 2. row ***************************
EmployeeId: 2
EmployeeName: Mike
EmployeeAge: 29
*************************** 3. row ***************************
EmployeeId: 3
EmployeeName: Sam
EmployeeAge: 31
*************************** 4. row ***************************
EmployeeId: 4
EmployeeName: Bob
EmployeeAge: 24
*************************** 5. row ***************************
EmployeeId: 5
EmployeeName: Carol
EmployeeAge: 28
5 rows in set (0.00 sec)

เมื่อไหร่ควรใช้ \G

  • ตารางมีคอลัมน์เยอะ (Horizontal scrolling ยาก)
  • ค่าข้อมูลยาว (TEXT, JSON, Long VARCHAR)
  • Debug/Debugging ต้องการดูค่าชัดเจน
  • ทำงานใน Terminal ที่แคบ

คำสั่ง MySQL Client อื่นๆ ที่มีประโยชน์

คำสั่งฟังก์ชัน
\GVertical output
\gExecute query (เหมือน ;)
\cCancel current query
\sShow status
\dDescribe table (ไม่ใช่ standard ต้องตั้งค่า)