14. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
14
12. กาหนดการติดตั้ง MySQL เป็น Service หนึ่งของ Windows โดยกาหนดชื่อ Service
“MySQL” แล ้วกดปุ่ ม Next
13. กาหนดรหัสผ่านของ root โดยพิมพ์รหัสในช่อง New Root Password และช่อง
Confirm รหัสผ่านดังกล่าว คลิกเลือกที่ Enable root access from remote
machines ในกรณีที่ต ้องการใช ้งาน MySQL จากเครื่องภายนอก จากนั้นกดปุ่ ม Next
20. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
20
กำรใช้งำน MySQL
หลังจากติดตั้งโปรแกรมเรียบร ้อยแล ้ว สามารถเข ้าใช ้งาน MySQL ได ้ดังนี้
1. คลิกเลือก Start >> MySQL >> MySQL Server 5.0 >> MySQL Command line
Client
2. จะปรากฎหน ้าจอของ MySQL Command Line Client
แล ้วให ้พิมพ์รหัสผ่านที่ใส่ในขั้นตอนการติดตั้ง แล ้วกด Enter
3. จะเข ้าสู่หน ้าจอการใช ้งาน MySQL โดยสังเกตุที่ mysql> ดังรูป
4. ในกรณีที่ต ้องการกาหนดรหัสผ่านใหม่ ให ้ใช ้คาสั่งดังนี้
UPDATE mysql.user SET password=password(‘1234’) WHERE user
= ‘root’; [กด Enter]
Flush privileges; [กด Enter]
21. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
21
MySQL Command Line
คำสั่งเพื่อเข้ำใช้งำน MySQL
mysql [db_name] -h hostname -u root –p
-h [ชื่อ server] ชื่อหรือ IP Address ของ Database Server เช่น
localhost หรือ 127.0.0.1
-u [user name] ชื่อผู ้ใช ้งานฐานข ้อมูล
-P [Port Number] เลข Port ของ MySQL เช่น 3333
-p [password] รหัสผ่าน
[db_name] ชื่อฐานข ้อมูลที่ต ้องการใช ้งาน
เช่น mysql –h localhost –u root
เรียกใช้งำนฐำนข้อมูล
use [db name];
คำสั่ง SHOW
SHOW DATABASES เป็นคาสั่งสาหรับแสดงรายละเอียด (Information)
ของ databases, tables, columns หรือ สถานะต่างๆ
ของ Server
SHOW TABLES แสดงรายชื่อ databases
ทั้งหมดในระบบฐานข ้อมูลแสดงรายชื่อ tables
ทั้งหมดของ current database
SHOW OPEN TABLES แสดงรายชื่อ tables ที่ถูกเปิดอยู่ในปัจจุบัน
SHOW COLUMNS แสดงรายละเอียดฟิลด์ทั้งหมดใน table นั้น
SHOW FIELDS ให ้ผลลัพธ์เช่นเดียวกับ SHOW COLUMNS
(ให ้ผลลัพธ์เช่นเดียวกับคาสั่ง DESCRIBE)
SHOW INDEX แสดงรายละเอียดว่ามีดัชนีอะไรบ ้างสาหรับ table นั้นๆ
22. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
22
SHOW KEYS ให ้ผลลัพธ์เช่นเดียวกับ SHOW INDEX
ขอควำมช่วยเหลือ
help หรือ h
เลิกใช้งำน MySQL
exit หรือ q
Data Definition Language
คำสั่งสร้ำงฐำนข้อมูล
CREATE DATABASE db_name
เป็นคาสั่งสร ้างฐานข ้อมู(database) ชื่อ db_name ขึ้นมาใหม่ ในกรณีที่มีdatabase นั้นอยู่แล ้ว
ก็จะปรากฏข ้อความแสดงความผิดพลาด (Error Message) Can't create database ' db_name '.
Database exists
คำสั่งลบฐำนข้อมูล
DROP DATABASE db_name
เป็นคาสั่งลบ database ชื่อ db_name ออกไปจากระบบ
30. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
30
กำรจัดกลุ่มข้อมูล
จัดกลุ่มข ้อมูลที่หมือนกัน ไว ้เป็นกลุ่มเดียวกันใช ้GROUP BY แล ้วระบุชื่อฟิลด์ที่ต ้องการจัดกลุ่ม
SELECT province , COUNT( id )
FROM phonebook
GROUP BY province ;
การเรียกดูข้อมูลขั้นสูง
การเรียกดูข ้อมูลจาก 2 ตารางขึ้นไป เช่น ประชากร( Person ) และข ้อมูลการรับบริการ(visit)
SELECT person.pid,prename,fname,lname,visitdate
FROM person,visit
WHERE person.pid=visit.pid
AND person.pcucodeperson=visit.pcucodeperson
Limit 10 ;
31. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
31
การเรียกใช ้ข ้อมูลจาก 2 ตาราง หลัง FROM จะตามด ้วยชื่อตารางคั่นระหว่างชื่อตารางด ้วย
Comma(,) เช่น FROM person,visit
ในส่วนของความสัมพันธ์ของทั้งสองตารางใส่หลัง where
โดยถ ้าทั้งสองตารางมีชื่อเหมือนกันจะต ้องจะต ้องใส่ชื่อตารางนาหน ้าตามด ้วยจุด(.)แล ้วจึงใส่ชื่อฟิลด์
โปรแกรมจะแสดงผลที่มีค่าตรงกันเท่านั้น เช่น WHERE person.pid=visit.pid AND
person.pcucodeperson=visit.pcucodeperson
หากไม่ระบุค่าความสัมพันธ์จะแสดงผลัพธ์เป็นผลคูณคาร์ทีเซียน
กำรกำหนดชื่อย่อของตำรำงในกำรเชื่อมตำรำง
SELECT p.pid,prename,fname,lname,visitdate
FROM person p ,visit v
WHERE p.pid = v.pid
AND p.pcucodeperson = v.pcucodeperson
Limit 10 ;
ประชากรแต่ละคนสามารถมารับบริการได ้มากกว่า 1 ครั้ง แต่ละครั้ง Diagได ้มากว่า 1โรค
SELECT person.pid,prename,fname,lname,visitdate ,diagcode
FROM person,visit
WHERE person.pid=visit.pid
AND person.pcucodeperson=visit.pcucodeperson
AND visit.pcucod=visitdiag.pcucode
AND visit.visitno=visitdiag.visitno
AND visitdate = ‘2010-02-01’
32. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
32
SELECT person.pid,prename,fname,lname,visitdate,diagcode,
diseasename
FROM person,visit,visitdiag,cdisease
WHERE person.pid=visit.pid
AND person.pcucodeperson=visit.pcucodeperson
AND visit.pcucod=visitdiag.pcucode
AND visit.visitno=visitdiag.visitno
AND visit.diagcode= cdisease.diseasecode
AND visitdate = ‘2010-02-01’
33. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
33
กำรเชื่อมตำรำงทำงขวำและทำงซ้ำย
SQL Left Join เป็นคาสั่งที่ใช ้สาหรับการระบุเงื่อนไขการเลือกใช ้ข ้อมูลในตาราง
โดยเงื่อนไขการทา Left join จะทาการเลือกข ้อมูลหลัก โดยใช ้ตารางที่อยู่ด ้านซ ้ายของคาสั่ง Join
เป็นตัวตั้ง และเลือกตารางด ้านขวาของคาสั่งที่มีค่าตรงกัน โดยที่ถ ้ามีข ้อมูลจากตารางด ้านซ ้าย
ไม่ตรงกับด ้านขวา ด ้านขวาจะแสดงข ้อมูลเป็น NULL
SQL Right Join เป็นคาสั่งที่ใช ้สาหรับการระบุเงื่อนไขการเลือกใช ้ข ้อมูลในตาราง
โดยเงื่อนไขการทา Right join จะทาการเลือกข ้อมูลหลัก โดยใช ้ตารางที่อยู่ด ้านขวาของคาสั่ง Join
เป็นตัวตั้ง และเลือกตารางด ้านซ ้ายของคาสั่งที่มีค่าตรงกัน โดยที่ถ ้ามีข ้อมูลจากตารางด ้านขวา
ไม่ตรงกับด ้านซ ้าย ด ้านซ ้ายจะแสดงข ้อมูลเป็น NULL
SELECT fname ,lname , occupa , occupaname
FROM person
LEFT JOIN coccupa
ON person.occupa = coccupa.occupacode ;
34. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
34
การเรียกใช ้ Subqueries
เป็นการเรียกดูข ้อมูลชุดหนึ่ง ภายในข ้อมูลอีกชุดหนึ่ง
แสดงรำยชื่อประชำกรที่ได้รับกำรวัด BP ในเดือนที่กำหนด ครั้งล่ำสุด
SELECT fname ,lname , occupa , occupaname
FROM person
WHERE
( SELECT pcucodeperson , pid
FROM visit
WHERE month(visitdate) = '02' and pressure is not null
GROUP BY pcucodeperson , pid
Having max(visitdate)
)
แสดงรำยชื่อประชำกรที่อยู่ในเขตรับผิดชอบที่ยังไม่เสียชีวิต
35. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
35
SELECT person.pid,prename,fname,lname,visitdate
FROM person
WHERE CONCAT(person.pid,person.pcucodeperson) NOT IN (
SELECT CONCA T(persondeath.pid,persondeath.pcucodeperson)
FROM persondeath
WHERE persondeath.deaddate IS NULL
OR persondeath.deaddate<= ‘2011-10-30’
)
36. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
36
การใช ้งาน Function
Arithmetic operator
+ บวก
- ลบ
* คูณ
/ หาร
% หารเอาเศษ
Comparison operator
= เท่ากับ
!= หรือ <> ไม่เท่ากับ
< น ้อยกว่า
<= น ้อยกว่าหรือเท่ากับ
> มากกว่า
>= มากกว่าหรือเท่ากับ
Exp BETWEEN Min AND Max มีค่าระหว่างค่าต่าสุด(Min) และค่าสูงสุด (MAX)
Exp IS NULL เป็นค่าว่าง
Exp IS NOT NULL ไม่เป็นค่าว่าง
Exp in(X1 ,X2,….,Xn) มีค่าอยู่ในชุดข ้อมูลที่เปรียบเทียบ
37. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
37
Logical operator
AND หรือ && และ
OR หรือ || หรือ
NOT หรือ ! ไม่
XOR Excusive OR
Mathematical Function
TRUNCATE ( expression, decimal ) ใช ้ตัดเศษทศนิยมของ expression ทิ้ง
ให ้เหลือจุดทศนิยม จานวนตาแหน่ง
decimal ที่กาหนด
ROUND(n , d) ใช ้ปัดตัวเลขทศนิยมของ n
ให ้มีจานวนจุดทศนิยมเท่ากับ d
ถ ้าไม่กาหนด d จะปัดเป็นจานวนเต็ม
FLOOR(n) ใช ้ปัดเศษของ expression ให ้มีค่าน ้อยลง
CEILING(n) ใช ้ปัดเศษของ expression ให ้มีค่าขึ้น
String Function
CONCAT ( char1, char2, ..., charN ) ใช ้นาสตริงของ char1, char2, ..., charN
นามาต่อกัน
LENGTH ( char ) ใช ้หาค่าความยาวของ char ที่กาหนด
LEFT ( sentence, length ) ใช ้คืนค่า sentence
โดยจะตัดสตริงให ้เหลือเพียงจานวนตัวอักษร
length โดยจะเริ่มนับจากทางซ ้าย
RIGHT ( sentence, length ) ใช ้คืนค่า sentence
โดยจะตัดสตริงให ้เหลือเพียงจานวนตัวอักษร
length โดยจะเริ่มนับจากทางขวา
41. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
41
Miscellaneous Function
SESSION_USER ( ) ใช ้คืนค่า username ที่ติดต่ออยู่
PASSWORD ( char ) ใช ้เข ้ารหัส ด ้วย function password
ENCRYPT ( char ) ใช ้เข ้ารหัสด ้วย function encrypt
ENCODE ( char, code ) ใช ้เข ้ารหัสด ้วย function encode โดยมีการกาหนด code
ด ้วย
DECODE ( char, code ) ใช ้ถอดรหัสด ้วย function decode
ซึ่งเป็นการถอดรหัสของค่าที่ได ้จากการเข ้ารหัส encode
โดยมีการกาหนด code ด ้วย
MD5 ( char ) ให ้เข ้ารหัสด ้วย function md5 โดยค่าที่จะได ้คือ
เลขฐานสิบหก ความยาว 32 ตัวอักษร
FORMAT ( num, decimal ) ใช ้จัดรูปแบบการแสดงผลตัวเลข แบบมี comma ทุกๆ 3
หลักของตัวเลข โดยมีการกาหนดจุดทศนิยมไว ้ด ้วย
VERSION ( ) ใช ้คืนค่า version ของ mysql
CONNECTION_ID( ) ใช ้คืนค่า id ของการเชื่อมต่อฐานข ้อมูล
Aggreation Function
COUNT ( column_name ) ใช ้นับจานวนแถวทั้งหมด แต่จะไม่นับค่าข ้อมูลที่เป็น
NULL โดยค่าที่คืนกลับมาจะเป็นชนิด int
SUM ( column_name ) ใช ้หาผลรวมของค่าข ้อมูลในทุกแถว
AVG ( column_name ) ใช ้หาค่าเฉลี่ยของค่าข ้อมูลในทุกแถว
MAX ( column_name ) ใช ้หาค่าสูงสุด เมื่อเทียบกับค่าข ้อมูลในทุกแถว
ถ ้าใช ้กับข ้อมูลที่เป็นตัวอักษร จะแสดงผลแถวแรกสุด
MIN ( column_name ) ใช ้หาค่าต่าสุด เมื่อเทียบกับค่าข ้อมูลในทุกแถว
ถ ้าใช ้กับข ้อมูลที่เป็นตัวอักษร จะแสดงผลแถวท ้ายสุด
concat
42. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
42
SELECT concat(fname ,’ ’,lname) AS name FROM person
case
SELECT concat(fname ,' ',lname) AS name ,
CASE WHEN sex='1' THEN 'ชาย'
WHEN sex='2' THEN 'หญิง'
ELSE '-' END AS sex
FROM person
อายุ(ปี)
SELECT getAgeYearNum(birth,current_date) AS age
FROM person
sum
SELECT SUM(CASE WHEN sex='1' THEN 1 ELSE null END) AS m,
SUM(CASE WHEN sex='2' THEN 1 ELSE null END) AS w ,
COUNT(sex ) AS total
FROM person
DATE_FORMAT
SELECT DATE_FORMAT( birth ,"%y-%m-%d") as birthdate
FROM person
47. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
47
train@203.157.4.27 ดังนั้นถ ้าติดต่อ MySQL โดยใช ้สคริปต์ PHP
ก็ต ้องเขียนคาสั่งในรูปแบบดังนี้
mysql_connect(“MySQLHost”,”train”,”รหัสผ่าน”);
คาสั่งกาหนดสิทธิการใช ้งาน GRANT มีรูปแบบดังนี้
GRANT สิทธิต่าง ๆ ON ชื่อฐานข ้อมูล.ชื่อตาราง TO
ชื่อผู ้ใช ้งาน@โฮสต์ [IDENTIFILE BY ‘รหัสผ่าน’]
GRANT ALL ON *.* TO train@203.157.4.27 IDENTIFILE BY
‘aBcXyz’;
สิทธิต่าง คือ สิทธิ (privilege) ที่จะสามารถทาได ้เช่น การเรียกดูข ้อมูล
(SELECT), การลดข ้อมูล(DELECT) หรือจะระบุสิทธิต่าง ๆ เป็น ALL
ก็ได ้ซึ่งหมายความว่าสามารถทาได ้ทุกอย่าง
2. ระดับฐำนข้อมูล (db level) ทั้งนี้ขึ้นอยู่กับว่ามีการระบุสิทธิต่าง ๆ ตามหลังคาสั่ง GRANT
ถ ้าระบุคาว่า ALL ก็หมายความว่าให ้สิทธิในกรเรียกใช ้ทุกคาสั่ง เช่น
GRANT สิทธิต่าง ๆ ON ชื่อฐานข ้อมูล.ชื่อตาราง TO
ชื่อผู ้ใช ้งาน@โฮสต์[IDENTIFILE BY ‘รหัสผ่าน’]
GRANT ALL ON *.* TO train@203.157.4.27 IDENTIFILE BY ‘aBcXyz’;
GRANT SELECT ON *.* TO User1@localhost IDENTIFILE BY ‘Password’;
หมายความว่า อนุญาตให ้user1 สามารถใช ้คาสั่ง SELECT
สาหรับเรียกดูข ้อมูลทุกตารางทุกฐานข ้อมูล
3. ระดับตำรำงข้อมูล (table level) สิทธิระดับนี้จะสัมพันธ์กับสิทธิระดับฐานข ้อมูล คือ
ถ ้าจะให ้สิทธิในการใช ้คาสั่งกับทุก ๆ ตารางใน ฐานข ้อมูลก็ระบุด ้วยเครื่องหมาย * เช่น
GIS.*
หมายถึงอนุญาตให ้มีสิทธิใช ้คาสั่งกับทุกตารางที่มีอยู่ในฐานข ้อมูลชื่อ GIS
GRANT SELECT ON db1.table1 TO User1@localhost IDENTIFILE BY
‘aBcXyz’;
หมายความว่าอนุญาตให ้user1 เรียกใช ้เฉพาะคาสั่ง SELECT
เพื่อดึงข ้อมูลจากตางรางชื่อ table1 ซึ่งอยู่ในฐานข ้อมูล db1 เท่านั้น
48. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
48
4. ระดับคอลัมน์ข้อมูล (Column level) เป็นสิทธิระดับที่ลึกลงไปจากสิทธิระดับอื่น ๆ อีก
โดยมีความสัมพันธ์กับสิทธิระดบฐานข ้อมูลและระดับตาราด ้วย
เพียงแต่วิธีการกาหนดสิทธิต ้องระบุในตางรางที่ชื่อ column_priv
ซึ่งตารางนี้อยู่ในฐานข ้อมูลmysql อีกทีฉะนั้นจึงต ้องใช ้คาสั่ง QL สั่งไปตรง ๆ ที่ตาราง
column_priv มิใช่กาหนดจาการใช ้คาสั่ง GRANT เหมือนกับสิทธิระดับอื่น ๆ
สิทธิต่ำง ๆ หรือ privilege หมายความว่า เมื่อเราอนุญาตให ้ผู ้ใช ้เข ้ามายังฐานข ้อมูล,
ตารางข ้อมูล, หรือเข ้ามายังMySQL ได ้แล ้ว สามารถใช ้คาสั่ง SQL ทาอะไรได ้บ ้าง
สิทธิต่าง ๆ การทางาน ตัวอย่างการใช ้งาน
SELECT เรียกดูรายการข ้อมูล SELECT * FROM table1
INSERT เพิ่มรายการข ้อมูล INSERT INTO table1 …
UPDATE ปรับปรุงแก ้ไขรายการข ้อมูล UPDATE table1 SET …
DELETE ลบรายการข ้อมูล DELETE FROM table1
ALTER แก ้ไขโครงสร ้างตารางข ้อมูล ALTER TABLE table1 ADD INDEX
index1(name)
CREATE สร ้างตารางหรือฐานข ้อมูลขึ้นมาใหม่ CREATE DATABASE database1
CREATE TABLE table1
DROP ลบตารางหรือฐานข ้อมูล DROP DATABASE database1
DROP TABLE table1
RELOAD สั่งให ้server เซ็นค่าต่าง ๆ ใหม่ FLUSH HOST (ปลดล็อคในกรณีที่
server มีการล็อค
ป้องกันการใช ้งานบางอย่าง เช่น
ป้องกันมิให ้มีผู ้ใช ้งาน
พร ้อมกันมาเกิดกาหนด)
SHUTDOWN อนุญาตให ้สั่งปิดการทางานของ
server
Mysqladmin shutdown
PROCESS ใช ้คาสั่งพิเศษกับ server เช่น
SHOW
SHOW PROCESSLIST
(ขอให ้แสดงรายการ โพรเซสที่
กาลังทางานอยู่)
FILE อ่าน – เขียน ไฟล์ที่ server ได ้ LOAD DATA INFILE ‘data.txt’ INTO
TABLE table1 (
โหลดข ้อมูลจากไฟล์ชื่อ data.txt
ไปบันทึกลงใน
ตารางข ้อมูล tabel1)
49. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
49
เมื่อคุณ install MySQL เสร็จ สิ่งแรกที่คุณควรทา คือการกา หนด password สา หรับ root
user โดยวิธีการ ดังนี้
shell> mysql -u root
mysql>use mysql;
mysql> UPDATE user SET
Password=PASSWORD('new_password') WHERE user='root';
mysql> FLUSH PRIVILEGES;
วิธีกำรกำหนด password
จากตัวอย่างในหัวข ้อที่ผ่านมาจะเห็นได ้ว่าเมื่อคุณ INSERT หรือ UPDATE password
คุณต ้องใช ้function PASSWORD() เพื่อเข ้ารหัส password เนื่องจากการกระทา เช.นนี้ ทา ให ้
password ถูกเก็บลง table ในแบบที่ถูกเข ้ารหัส เพื่อความปลอดภัยของข ้อมูล หากคุณลืมใช ้function
PASSWORD() , password ก็จะถูกเก็บในรูปแบบของตัวหนังสือ (plaintext)
การนาเข้าข้อมูล
รูปแบบการใช ้LOAD DATA INFILE มีดังนี้
LOAD DATA [LOW_PRIORITY] [LOCAL] INFILE 'file_name.txt'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[FIELDS
[TERMINATED BY 't']
[OPTIONALLY] ENCLOSED BY '']
[ESCAPED BY '' ]]
[LINES TERMINATED BY 'n']
[IGNORE number LINES]
[(col_name,...)]
LOCAL หมายถึง file ที่ถูกอ่านถูกเก็บไว ้ที่ client ซึ่งจะทา ให ้เครื่องทา งานช ้าลง เนื่องจาก
server จะต ้องไปอ่าน file จากเครื่อง client ถ ้าไม่มีคา ว่า LOCAL , file จะต ้องถูกเก็บไว ้ที่ server และ
การอ่านtext files ที่อยู่บน server file จะต ้องอยู่ใน database directory หรือเป็น file ที่ user
ทุกคนสามารถเปิดอ่านได ้นั่นคือถ ้าคุณต ้องการอ่าน file จาก server คุณจะต ้องกา หนด file privilege
บน server host.
ถ ้า LOW_PRIORITY ถูกกา หนด หมายถึงการทา คา สั่ง LOAD DATA
จะถูกเลื่อนออกไปจนกระทั่งไม่มี clients เครื่องใดอ่านข ้อมูลจากตาราง การหาตาแหน่งของ file บน
server นั้น server จะต ้องทราบ pathname ของ file เพื่อหาตา แหน่งของ file โดยใช ้กฎดังนี้ :
ถ ้า pathname เป็นแบบ absolute path, server จะใช ้pathname ในการค ้นหา file
50. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
50
ถ ้า pathname เป็นแบบ relative pathname, server จะค ้นหา file นั้นโดยอ ้างอิง จาก
directory ของ data ใน server
ถ ้า file name ไม่ได ้กา หนด path มาด ้วย, server จะค ้นหา file จาก database directory
ของdatabase ที่ใช ้ปัจจุบัน
IGNORE number LINES ใช ้สา หรับทา ให ้MySQL ไม่สนใจข ้อมูล number
บรรทัดของส่วนหัวข ้อ file เช่น ขอ ้มลู บรรทัดแรกเป็นชื่อ ของ column เป็นต ้น
LOAD DATA INFILE "/tmp/file_name" into table test IGNORE 1
การ Export ข้อมูล
สามารถใช ้คาสั่งเพื่อส่งออกข ้อมูลที่ต ้องการออกเป็น Text File ได ้ตามตัวอย่าง
SELECT idcard , fname ,lname
FROM person
INTO OUTFILE '/tmp/exdata.txt';
SELECT idcard , fname ,lname
INTO OUTFILE '/tmp/exdata.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY ''
LINES TERMINATED BY 'n'
FROM person
SELECT idcard , fname ,lname เลือกฟิลด์ข ้อมูลที่ต ้องการส่งออก
INTO OUTFILE '/tmp/exdata.csv' ตาแหน่งและชื่อไฟล์ที่ต ้องการส่งออก
FIELDS TERMINATED BY ',' คั้นข ้อมูลแต่ละฟิลด์ด ้วยคอมมา
ENCLOSED BY '"' ครอบข ้อมูลด ้วยเครื่องหมายคาพูด(“”)
ESCAPED BY '' ใส่ Escape Character ด ้วย Back Slash()
LINES TERMINATED BY 'n' ขึ้นบรรทัดใหม ้ของแต่ละรายการด ้วย(n)
FROM person ชื่อตารางข ้อมูลที่ต ้องการส่งออก
55. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
55
การใช ้งาน MySQL Administrator
โปรแกรม MySQL Administrator เป็นโปรแกรม
ที่ใช ้งานเพื่อช่วยในการบริหารจัดการฐานข ้อมูล MySQL
เริ่มใช ้งานโปรแกรม
1. คลิก Start >> MySQL-JHCIS >> เลือก MySQL Administrator
2. Connect to MySQL
กาหนดค่าเพื่อติดต่อกับฐานข ้อมูล MySQL ดังรูป แล ้วกดปุ่ ม OK
Store Connection สามารถสร ้าง Connection ไว ้ใช ้งานได ้โดย
กดปุ่ ม
Server Host ชื่อ หรือ IP Address ของ MySQL Server
ถ ้าเป็นเครื่องเดียวกับ Server ใช ้ชื่อ Localhost หรือ
127.0.0.1
Port Port Number ที่ใช ้สาหรับ JHCIS ใช ้port 3333
โดยปกติ MySQL ใช ้port 3306
Username ชื่อที่เข ้าใช ้งาน MySQL
Password รหัสผ่านของ Username ที่เข ้าใช ้งาน MySQL
75. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
75
สารบัญ
แนะนาระบบจัดการฐานข ้อมูล 1
การติดตั้งและใช ้งาน MySQL
ประเภทข ้อมูลใน MySQL
Start and Stop MySQL Service
MySQL Command Line
Data Definition Language
Data Manipulation Language
Data Retrieval Language
การเรียกดูข ้อมูลชั้นสูง
SQL Function
การสร ้าง View
การกาหนดสิทธิในการใช ้งานฐานข ้อมูล
การสารองข ้อมูล
การนาข ้อมูลสารองใช ้งาน
การเพิ่มประสิทธิภาพ และการซ่อมแซมตารางข ้อมูล
การใช ้งาน MySQL Administrator
การใช ้งาน MySQL Query Browser
โครงสร ้างฐานช ้อมูล JHCIS
76. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
76
ScripSQL ForJHCIS
Cr.สสอไม้แก่น
link=> http://itmaikaenhos.blogspot.com/p/scrip-sql-for-jhcis.html
รายชื่อปชช. อายุ 35 ปี ขึ้นไป ที่ยังไม่ได้รับการคัดกรองเบาหวาน-ความดัน
==========================================================================
===
SELECT
person.pidas'HN',person.fname as'ชื่อ',...person.lname as'นามสกุล',person.hnomoi as
'บ้านเลขที่',person.mumoi as'หมู่ที่',
#person.typelive#,
getAgeYearNum(birth,CURDATE())as'อายุ'
FROM
person
INNERJOIN ncdpersonscreenallON person.pcucodeperson=ncdpersonscreenall.pcucodeAND
person.pid=ncdpersonscreenall.pid
WHERE
person.typelive IN ( 1,3)
ANDperson.pidNOTIN (selectpersondeath.pidfrompersondeath)
ANDgetAgeYearNum(birth,CURDATE()) >='35'
ANDperson.pidNOTIN (selectpersonchronic.pidfrompersonchronic)
ANDncdpersonscreenall.visitnoNOTBETWEEN '20130401'AND '2014331'
GROUP BY
person.pid
code ตอบตัวชี้วัด QOFเรื่อง ร้อยละของหญิงตั้งครรภ์ได้รับการฝากครรภ์ครบ5ครั้งตามเกณฑ์
77. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
77
==========================================================================
======
SELECT p.pid,
concat(p.prename,'',p.fname,'',p.lname) aspersonname,
p.idcard,timestampdiff(year,p.birth,vp.lmp)ASage,...
p.hnomoi,p.villnoAS หมู่,vp.lmp,vp.edc,t.datedeliverAS วันคลอด,t.pregno,
t1.pregage ASANC1_p_age,t1.datecheckASANC1_d_check,
t2.pregage ASANC2_p_gag,t2.datecheckASANC2_d_check,
t3.pregage ASANC3_p_gag,t3.datecheckASANC3_d_check,
t4.pregage ASANC4_p_gag,t4.datecheckASANC4_d_check,
t5.pregage ASANC5_p_gag,t5.datecheckASANC5_d_check
FROM
(SELECT * FROM visitancdelivervWHERE v.datedeliverBETWEEN '2013-10-01' AND'2014-03-31'
ORDER BY v.datedeliver) t
LEFT JOIN visitancpregnancyvpON vp.pcucodeperson=t.pcucodepersonANDvp.pid=t.pidAND
vp.pregno=t.pregno
INNERJOIN (SELECT ps.*,village.villnoFROMpersonps
INNERJOIN house ON ps.pcucodeperson=house.pcucode ANDps.hcode=house.hcode
INNERJOIN village ON house.pcucode =village.pcucode ANDhouse.villcode =village.villcodewhere
right(house.villcode,2)!='00') pON p.pid=t.pidANDp.pcucodeperson=t.pcucodeperson
LEFT JOIN (SELECT* FROMvisitancWHERE pregage<=12GROUP BY pid,pregno) t1ON t1.pid=t.pid
ANDt1.pregno=t.pregno
LEFT JOIN (SELECT* FROMvisitancWHERE pregage BETWEEN 16 AND 20 GROUP BY pid,pregno) t2
ON t2.pid=t.pidANDt2.pregno=t.pregno
LEFT JOIN (SELECT* FROMvisitancWHERE pregage BETWEEN 24 AND 28 GROUP BY pid,pregno) t3
ON t3.pid=t.pidANDt3.pregno=t.pregno
LEFT JOIN (SELECT* FROMvisitancWHERE pregage BETWEEN 30 AND 34 GROUP BY pid,pregno) t4
ON t4.pid=t.pidANDt4.pregno=t.pregno
LEFT JOIN (SELECT* FROMvisitancWHERE pregage BETWEEN 36 AND 40 GROUP BY pid,pregno) t5
ON t5.pid=t.pidANDt5.pregno=t.pregno
78. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
78
จานวนคัดกรองและการตีค่าความเสี่ยงverbal
==========================================================================
====
SELECT CASEWHEN age_yearbetween'15'and'34' THEN 'อายุ15_34' WHEN age_yearbetween'35'
and '59' THEN 'อายุ35_59' WHEN age_year>= '60' THEN 'อายุ>60'ELSE null ENDAS 'Grage',
case whenncd_person.sex ='1' then'ชาย'whenncd_person.sex ='2' then'หญิง'else'ไม่ระบุ'endas
grsex,
sum(CASEWHEN age_year>= '15' THEN 1 ELSE 0 END) AS 'จานวนคัดกรอง',
sum(CASEWHEN age_yearbetween'15'and'34' and chronic_flag='0'and(screen_q1+screen_q2+
screen_q3+(blackarmpit='1') )>='3' THEN 1 ELSE 0 END) AS'verbal<15ปี',
sum(CASEWHEN age_year> = '35' and chronic_flag='0'and (screen_q1+screen_q2+screen_q3+
screen_q4+screen_q5+screen_q6)>='1' THEN 1 ELSE 0 END) AS'verbal>35ปี',
sum(CASEWHEN chronic_flag='0'and bsl> '0' THEN 1 ELSE 0 END) AS'ได้รับการเจาะDTX',
sum(CASEWHEN chronic_flag='0'and bsl < '100' THEN 1 ELSE 0 END) AS'bsl<100',
sum(CASEWHEN chronic_flag='0'and bsl between'100'and'125' THEN 1 ELSE 0 END) AS 'bsl100-
125',sum(CASEWHEN chronic_flag='0'andbsl >= '126' THEN 1 ELSE 0 END) AS 'bsl>126',
sum(CASEWHEN chronic_flag='0'and ((hbp_s2between'120'and'139') or (hbp_d2between'80'and
'89')) THEN 1 ELSE 0 END) AS'BP120/80-139/89',
sum(CASEWHEN chronic_flag='0'and (hbp_s2>= '140' and hbp_d2>= '90') THEN 1 ELSE 0 END) AS
'BP>140/90',
sum(CASEWHEN chronic_flag='0'and (hbp_s1< '120' andhbp_d1 < '80') THEN 1 ELSE 0 END) AS
'BP<120/80',
sum(CASEWHEN ncd_person.sex ='1' and chronic_flag='0'and ncd_person_ncd_screen.waist>='90'
THEN 1 ELSE 0 END) AS 'รอบเอวเกินชาย',
sum(CASEWHEN ncd_person.sex ='2' and chronic_flag='0'and ncd_person_ncd_screen.waist>='80'
THEN 1 ELSE 0 END) AS 'รอบเอวเกินหญิง',
sum(CASEWHEN chronic_flag='0'and ncd_person_ncd_screen.bmi >='25' THEN 1 ELSE 0 END) AS
'BMI>25'
FROM
79. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
79
ncd_person_ncd_screen
INNERJOIN ncd_personON ncd_person_ncd_screen.pcucode=ncd_person.pcucode AND
ncd_person_ncd_screen.pid=ncd_person.pid
INNERJOIN ncd_person_ncdON ncd_person_ncd_screen.pcucode=ncd_person_ncd.pcucode AND
ncd_person_ncd_screen.pid=ncd_person_ncd.pid
WHERE ncd_person_ncd_screen.screen_datebetween'2013-10-01'and'2014-11-30'
and SUBSTRING(ncd_person.village,7,2)<>'00'
groupby Grage,grsex
Code จานวนคัดกรองแยกverbal แยกกลุ่มสีแยกเพศแยกอายุ
Code จานวนคัดกรองแยกตามverbal 6ข้อแยกกลุ่มสีแยกเพศแยกอายุ
มีเงื่อนไขคือ ผู้ที่อายุ15 ปีขึ้นไป (อายุ ณวันที่ 1 ตุลาคม2556)ที่มีชีวิตทุกคน
(ถ้าคุณเอากลุ่มป่วยมาบันทึกก็จะนับไปด้วย)และมีTypearea1+3ที่ได้รับการคัดกรองNCDตั้งแต่วันที่1 ตุลาคม
2556 ถึงวันที่30 กันยายน2557
==========================================================================
====
SELECT CASEWHEN person.sex ='1' THEN 'ชาย'WHEN person.sex ='2' THEN 'หญิง'ELSE'ไม่ระบุ'END AS
groupsex,
CASE WHEN age_yearbetween'15'and'34' THEN '15-34'WHEN age_yearbetween'35'and'59' THEN
'35-39'WHEN age_year>='60' THEN '>60'ELSE'ไม่ระบุ'END ASgrouppop,
count(ncd_person_ncd_screen.pid) as'จานวนคัดกรอง',
sum(CASEWHEN screen_q1='1' THEN '1' ELSE null END) AS 'ญาติเบาหวาน',
sum(CASEWHEN screen_q2='1' THEN '1' ELSE null END) AS 'รอบเอว+BMIเกิน',
sum(CASEWHEN screen_q3='1' THEN '1' ELSE null END) AS 'BP>140/90',
sum(CASEWHEN screen_q4='1' THEN '1' ELSE null END) AS 'ประวัติน้าตาลสูง',
80. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
80
sum(CASEWHEN screen_q5='1' THEN '1' ELSE null END) AS 'Tri>250',sum(CASEWHEN screen_q6=
'1' THEN '1' ELSE null END) AS'เบาหวานขณะตั้งครรภ์',
sum(CASEWHEN blackarmpit= '1' THEN '1' ELSE null END) AS 'รอยพับดา',
sum(CASEWHEN htfamily='1' THEN '1' ELSE null END) AS 'ญาติBPสูง',
sum(CASEWHEN ((hbp_s1<'120' and hbp_d1<'80')OR (hbp_s2<'120' and hbp_d2<'80')) THEN '1'
ELSE null END) AS'HTขาว',
sum(CASEWHEN ((hbp_s2between'120'and'139') or (hbp_d2between'80'and'89')) THEN '1' ELSE
null END) AS'HTเขียว',sum(CASEWHEN bsl < '100' THEN '1' ELSE null END) AS 'DMขาว',
sum(CASEWHEN bsl between'100'and'125' THEN '1' ELSE null END) AS 'DMเขียว'
FROM
ncd_person_ncd_screenINNERJOIN ncd_personON ncd_person_ncd_screen.pcucode =
ncd_person.pcucode ANDncd_person_ncd_screen.pid=ncd_person.pid
INNERJOIN personON ncd_person_ncd_screen.pcucode =person.pcucodepersonAND
ncd_person_ncd_screen.pid=person.pid
WHERE
ncd_person_ncd_screen.screen_date between'2013-10-01'and'2014-09-30'
and SUBSTRING(ncd_person.village,7,2)!='00'and person.typelive in('1','3') and
CONCAT(person.pid,person.pcucodeperson) notin(SELECT
CONCAT(persondeath.pid,persondeath.pcucodeperson)
FROMpersondeath)
groupby grouppop,groupsex
=========================================================================
Code เป้าหมายคัดกรองแยกเพศแยกอายุ
จานวนกลุ่มเป้าหมายคัดกรองNCDแยกเพศและกลุ่มอายุมีเงื่อนไขคือประชากรที่อายุ15ปีขึ้นไป(อายุ ณ วันที่ 1
ตุลาคม2556) ทุกคนที่ยังมีชีวิตและมีTypearea
1+3=======================================================================
======
SELECT
81. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
81
CASE WHEN person.sex='1' THEN 'ชาย'WHEN person.sex='2' THEN 'หญิง'ELSE'ไม่ระบุ'END AS
groupsex,
SUM(CASE WHEN person.typelivein('1','3') ANDGetAgeYearNum(person.birth,'2013-10-01')
between'15'and'34' THEN 1 ELSE '' END) as '15-34',
SUM(CASE WHEN person.typelivein('1','3') ANDGetAgeYearNum(person.birth,'2013-10-01')
between'35'and'59' THEN 1 ELSE '' END) as '35-59',
SUM(CASE WHEN person.typelivein('1','3') ANDGetAgeYearNum(person.birth,'2013-10-01') >=60
THEN 1 ELSE '' END) as '>60'
FROMperson
INNERJOIN house ON person.pcucodeperson=house.pcucode ANDperson.hcode =house.hcode
INNERJOIN village ON house.pcucode =village.pcucode ANDvillage.villcode =house.villcode
WHERE CONCAT(person.pid,person.pcucodeperson) notin(SELECT
CONCAT(persondeath.pid,persondeath.pcucodeperson)
FROMpersondeath) andSUBSTRING(house.villcode,7,2)!='00'
groupby groupsex
Code จานวนคัดกรองแยกกลุ่มสีอายุ15- 34 ปี
=========================================================================
SELECT
(SELECT
SUM(CASE WHEN person.typelivein('1','3') ANDGetAgeYearNum(person.birth,'2013-10-01')
between'15'and'34' THEN 1 ELSE '' END)
FROM
person
INNERJOIN house ON person.pcucodeperson=house.pcucode ANDperson.hcode =house.hcode
INNERJOIN village ON house.pcucode =village.pcucode ANDvillage.villcode =house.villcode
WHERE (person.dischargetypeisnull orperson.dischargetype=9) and
SUBSTRING(house.villcode,7,2)!='00'
and concat(person.pid,person.pcucodeperson) notin(selectconcat(personchronic.pid,
personchronic.pcucodeperson) from personchronicwhere personchronic.chroniccodebetween'I10'
and 'I15.9' and personchronic.chroniccode between'E10'and'E15')) as 'Pop 15 ปี',
82. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
82
count(ncd_person_ncd_screen.pid) as'จานวนคัดกรอง',
sum(CASEWHEN (hbp_s1 <'120' and hbp_d1<'80')OR (hbp_s2 <'120' and hbp_d2<'80') THEN '1'
ELSE null END) AS'HTขาว',
sum(CASEWHEN (hbp_s2between'120'and'139') or (hbp_d2between'80'and'89') THEN '1'
ELSE null END) AS'HTเขียว',
sum(CASEWHEN bsl < '100' THEN '1' ELSE null END) AS'DMขาว',
sum(CASEWHEN bsl between'100'and'125' THEN '1' ELSE null END) AS'DMเขียว'
FROM
ncd_person_ncd_screen INNERJOIN ncd_person ON ncd_person_ncd_screen.pcucode =
ncd_person.pcucode ANDncd_person_ncd_screen.pid=ncd_person.pid
INNERJOIN person ON ncd_person_ncd_screen.pcucode=person.pcucodepersonAND
ncd_person_ncd_screen.pid=person.pid
WHERE
age_yearbetween'15'and'34' and ncd_person_ncd_screen.screen_datebetween'2013-10-01'
and '2014-09-30'
andSUBSTRING(ncd_person.village,7,2)!='00'and person.typelivein('1','3') and
CONCAT(person.pid,person.pcucodeperson) notin(SELECT
CONCAT(persondeath.pid,persondeath.pcucodeperson)
FROMpersondeath);
==========================================================================
====
อายุ 35 - 59 ปี
==========================================================================
====
SELECT
(SELECT
SUM(CASE WHEN person.typelivein('1','3') ANDGetAgeYearNum(person.birth,'2013-10-01')
between'35'and'59' THEN 1 ELSE '' END)
FROM
person
INNERJOIN house ON person.pcucodeperson=house.pcucode ANDperson.hcode =house.hcode
83. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
83
INNERJOIN village ON house.pcucode =village.pcucode ANDvillage.villcode =house.villcode
WHERE (person.dischargetypeisnull orperson.dischargetype =9) and
SUBSTRING(house.villcode,7,2)!='00'
and concat(person.pid,person.pcucodeperson) notin(selectconcat(personchronic.pid,
personchronic.pcucodeperson) from personchronic
where personchronic.chroniccode between'I10'and'I15.9' and personchronic.chroniccodebetween
'E10' and'E15')) as 'Pop 15 ปี',
count(ncd_person_ncd_screen.pid) as'จานวนคัดกรอง',
sum(CASEWHEN (hbp_s1 <'120' and hbp_d1<'80')OR (hbp_s2 <'120' and hbp_d2<'80') THEN '1'
ELSE null END) AS'HTขาว',
sum(CASEWHEN (hbp_s2between'120'and'139') or (hbp_d2between'80'and'89') THEN '1'
ELSE null END) AS'HTเขียว',
sum(CASEWHEN bsl < '100' THEN '1' ELSE null END) AS'DMขาว',
sum(CASEWHEN bsl between'100'and'125' THEN '1' ELSE null END) AS'DMเขียว'
FROM
ncd_person_ncd_screen INNERJOIN ncd_person ON ncd_person_ncd_screen.pcucode =
ncd_person.pcucode ANDncd_person_ncd_screen.pid=ncd_person.pid
INNERJOIN person ON ncd_person_ncd_screen.pcucode=person.pcucodepersonAND
ncd_person_ncd_screen.pid=person.pid
WHERE
age_yearbetween'35'and'59' and ncd_person_ncd_screen.screen_datebetween'2013-10-01'
and '2014-09-30'
andSUBSTRING(ncd_person.village,7,2)!='00'and person.typelivein('1','3') and
CONCAT(person.pid,person.pcucodeperson) notin(SELECT
CONCAT(persondeath.pid,persondeath.pcucodeperson)
FROMpersondeath);
==========================================================================
====
อายุ 60 ปีขึ้นไป
==========================================================================
====
84. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
84
SELECT
(SELECT
SUM(CASE WHEN person.typelivein('1','3') ANDGetAgeYearNum(person.birth,'2013-10-01') >='60'
THEN 1 ELSE '' END)
FROM
person
INNERJOIN house ON person.pcucodeperson=house.pcucode ANDperson.hcode =house.hcode
INNERJOIN village ON house.pcucode =village.pcucode ANDvillage.villcode =house.villcode
WHERE (person.dischargetypeisnull orperson.dischargetype= 9) and
SUBSTRING(house.villcode,7,2)!='00'
and concat(person.pid,person.pcucodeperson) notin(selectconcat(personchronic.pid,
personchronic.pcucodeperson) from personchronic
where personchronic.chroniccode between'I10'and'I15.9' and personchronic.chroniccodebetween
'E10' and'E15')) as 'Pop 15 ปี',
count(ncd_person_ncd_screen.pid) as'จานวนคัดกรอง',
sum(CASEWHEN (hbp_s1 <'120' and hbp_d1<'80')OR (hbp_s2 <'120' and hbp_d2<'80') THEN '1'
ELSE null END) AS'HTขาว',
sum(CASEWHEN (hbp_s2between'120'and'139') or (hbp_d2between'80'and'89') THEN '1'
ELSE null END) AS'HTเขียว',
sum(CASEWHEN bsl < '100' THEN '1' ELSE null END) AS'DMขาว',
sum(CASEWHEN bsl between'100'and'125' THEN '1' ELSE null END) AS'DMเขียว'
FROM
ncd_person_ncd_screen INNERJOIN ncd_person ON ncd_person_ncd_screen.pcucode =
ncd_person.pcucode ANDncd_person_ncd_screen.pid=ncd_person.pid
INNERJOIN person ON ncd_person_ncd_screen.pcucode=person.pcucodepersonAND
ncd_person_ncd_screen.pid=person.pid
WHERE
age_year>= '60' and ncd_person_ncd_screen.screen_datebetween'2013-10-01'and'2014-09-30'
andSUBSTRING(ncd_person.village,7,2)!='00'and person.typelivein('1','3') and
CONCAT(person.pid,person.pcucodeperson) notin(SELECT
CONCAT(persondeath.pid,persondeath.pcucodeperson)
FROMpersondeath);
85. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
85
Code 7 Colourกลุ่มคัดกรอง
Code รายชื่อกลุ่มคัดกรองแยกตามกลุ่มสีเปลี่ยนวันที่ต้องการ[between'วันที่เริ่มต้น'and'วันที่สิ้นสุด']
=========================================================================
SELECT
person.pidASpid,
concat(ctitle.titlename,person.fname,' ',person.lname)aspname,
age_year,
person.idcardASpcid,
person.sexASsex,
person.hnomoi AShno,
person.mumoi ASmu,
hbp_s1,hbp_s2,hbp_d1,hbp_d2,
DATE_FORMAT(screen_date,'%Y-%m-%d') asscreen_date,
max(CASEWHEN (hbp_s1 <'120' and hbp_d1<'80')OR (hbp_s2 <'120' and hbp_d2<'80') THEN "/"
ELSE null END) AS'NBp',
max(CASEWHEN (hbp_s2between'120'and'139') or (hbp_d2between'80'and'89') THEN "/"
ELSE null END) AS'HBp', max(CASEWHEN (hbp_s2>= '140' or hbp_d2>= '90') THEN "/" ELSE null
END) AS 'RoHT',
bsl,
max(CASEWHEN bsl < '100' THEN "/" ELSE null END) AS 'NDM',
max(CASEWHEN bsl between'100'and'125' THEN "/" ELSE null END) AS'HDM',
max(CASEWHEN bsl >= '126' THEN "/" ELSE null END) AS'RoDM'
FROM
ncd_person_ncd_screenINNERJOIN personON ncd_person_ncd_screen.pid=person.pid
INNERJOIN ctitle ON person.prename =ctitle.titlecode
INNERJOIN ncd_person ON ncd_person_ncd_screen.pcucode=ncd_person.pcucode AND
ncd_person_ncd_screen.pid=ncd_person.pid
WHERE
age_year>= '15' and ncd_person_ncd_screen.screen_datebetween'2013-10-01'and'2014-09-30'
and concat(ncd_person_ncd_screen.pid,ncd_person_ncd_screen.pcucode) notin
(selectconcat(ncd_person_ncd_hist.pid,ncd_person_ncd_hist.pcucode)
94. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
94
FROM
visitlabcancer INNERJOIN person ON visitlabcancer.pcucodeperson=person.pcucodepersonAND
visitlabcancer.pid=person.pid
where getAgeYearNum(birth,visitlabcancer.datecheck) between'30'and'60'and sex = '2' and
visitlabcancer.datecheckbetween'2013-10-01'and'2014-09-30'
and person.typelivein('1','3') andperson.dischargetype !='1'
groupby GroupMonth
orderby 'ปี' ;
จานวนการให้หัตถการ
==========================================================================
=======selectdrugname,count(distinctvisit.pcucode,visit.pid) as'คน',count(visitdrug.unit)as'ครั้ง'
from
visitleftjoinvisitdrugonvisit.visitno=visitdrug.visitnoandvisit.pcucode=visitdrug.pcucode
leftjoincdrugon visitdrug.drugcode =cdrug.drugcode
where cdrug.drugtype='02'andcdrug.drugtypesub='3' and visit.visitdate between'2013-07-01'and
'2014-10-31'
groupby cdrug.drugcode
Code จานวนกลุ่มเป้าหมายตามกลุ่มอายุ
==========================================================================
=======
SELECT house.villcode,
SUM(CASE WHEN person.typelivein('1','3') ANDROUND(DATEDIFF('2013-10-01',person.birth)/30)<=
35.99 THEN 1 ELSE '' END) AS '0-2ปี',
SUM(CASE WHEN person.typelivein('1','3') ANDROUND(DATEDIFF('2013-10-01',person.birth)/30)<=
47.99 THEN 1 ELSE '' END) AS '0-3ปี',
SUM(CASE WHEN person.typelivein('1','3') ANDROUND(DATEDIFF('2013-10-01',person.birth)/30)
BETWEEN 36 AND 71.99 THEN 1 ELSE '' END) AS '3-5ปี',
SUM(CASE WHEN person.typelivein('1','3') ANDROUND(DATEDIFF('2013-10-01',person.birth)/30)
BETWEEN 72 AND 143.99THEN 1 ELSE '' END) AS'6-12ปี',
95. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
95
SUM(CASE WHEN person.typelivein('1','3') ANDGetAgeYearNum(person.birth,'2013-10-01') >=15
THEN 1 ELSE '' END) AS '15ปีขึ้นไป',
SUM(CASE WHEN person.sex='2'ANDperson.typelive IN ('1','3') AND
GetAgeYearNum(person.birth,'2013-10-01') BETWEEN 30 AND59 THEN 1 ELSE '' END) AS'หญิง30-
60ปี',
SUM(CASE WHEN person.typelivein('1','3') ANDGetAgeYearNum(person.birth,'2013-10-01') >=60
THEN 1 ELSE '' END) AS '60ปีขึ้นไป',
SUM(CASE WHEN person.typelivein('1','3') ANDGetAgeYearNum(person.birth,'2013-10-01')
between 15and34 THEN 1 ELSE '' END) AS '15_34',
SUM(CASE WHEN person.typelivein('1','3') ANDGetAgeYearNum(person.birth,'2013-10-01')
between 35and59 THEN 1 ELSE '' END) AS '35_59'
FROMperson
INNERJOIN house ON person.pcucodeperson=house.pcucode
ANDperson.hcode =house.hcode
INNERJOIN village ON house.pcucode =village.pcucode
ANDvillage.villcode =house.villcode
WHERE (person.dischargetypeisnull orperson.dischargetype= 9) and
SUBSTRING(house.villcode,7,2)!='00'
GROUP BY house.villcode;
Code ประชากรจาแนกตามTypearea
==========================================================================
=======
select
sum(case whenperson.typelive='1'then1else 0end) as '1=มีชื่อและอาศัยอยู่จริง'
,sum(case whenperson.typelive='2'then1else 0end) as'2=มีชื่อฯแต่ไม่อยู่จริง'
,sum(case whenperson.typelive='3'then1else 0end) as'3=ไม่มีชื่อฯแต่อยู่จริง'
,sum(case whenperson.typelive='4'then1else 0end) as'4=คนนอกเขตมารับบริการ'
fromperson
innerjoinhouse onperson.hcode=house.hcodeandperson.pcucodeperson=house.pcucode
and concat(person.pid,person.pcucodeperson)
not in(selectconcat(persondeath.pid,persondeath.pcucodeperson)
96. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
96
frompersondeath
where deaddate<=curdate() ordeaddate isnotnull)
จานวนผู้พิการแยกรายหมู่
==========================================================================
=======
SELECT village.villcode,villname,count(person.pid)as'พิการ'
FROM
personINNERJOIN personunable ON person.pcucodeperson=personunable.pcucodeperson
innerjoinhouse onperson.hcode=house.hcodeandperson.pcucodeperson=house.pcucode
innerjoinvillageonhouse.villcode =village.villcodeandhouse.pcucode =village.pcucode
ANDperson.pid=personunable.pid
where concat(person.pid,person.pcucodeperson)notin
(selectconcat(persondeath.pid,persondeath.pcucodeperson)
frompersondeathwhere deaddate<=curdate()ordeaddate isnotnull) and
SUBSTRING(house.villcode,7,2)<>'00'
and person.typelivein('1','3')
groupby village.villcode;
พฤติกรรมสุขภาพ
==========================================================================
=======
select
village.villcode,villname
,sum(case whenciga= 0 then1 else 0end) as 'ไม่สูบ'
,sum(case whenciga= 1 then1 else 0end) as 'สูบนานๆครั้ง'
,sum(case whenciga= 2 then1 else 0end) as 'สูบประจา'
,sum(case whenwisky=0then1 else 0 end) as'ไม่ดื่ม'
,sum(case whenwisky=1then1else 0end) as 'ดื่มนานๆครั้ง'
,sum(case whenwisky=2then1 else 0 end) as'1-2ครั้งต่อเดือน'
97. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
97
,sum(case whenwisky=3then1 else 0 end) as'1-2ครั้งต่อสัปดาห์'
,sum(case whenwisky=4then1 else 0 end) as'3-4ครั้งต่อสัปดาห์'
,sum(case whenwisky=5then1 else 0 end) as'ดื่มทุกวัน'
,sum(case whenexercise=0 then1 else 0end) as 'ไม่ออกกาลังกาย'
,sum(case whenexercise=1 then1 else 0end) as 'ออก<3วัน'
,sum(case whenexercise=2 then1 else 0end) as 'ออก 3-5วัน'
,sum(case whenexercise=3 then1 else 0end) as 'ออก>5วัน'
frompersonleftjoinpersonbehavioronperson.pid=personbehavior.pidand
person.pcucodeperson=personbehavior.pcucodeperson
leftjoinhouse onperson.hcode=house.hcode andperson.pcucodeperson=house.pcucode
leftjoinvillageon house.villcode =village.villcodeandhouse.pcucode =village.pcucode
where substring(house.villcode,7,2)!='00'
and GetAgeYearNum(person.birth,curdate()) between'15'and'19'
and concat(person.pid,person.pcucodeperson) notin(select
concat(persondeath.pid,persondeath.pcucodeperson) frompersondeathwhere
persondeath.pcucodeperson=person.pcucodepersonand(persondeath.deaddate isnull or
persondeath.deaddate<=curdate()))
groupby village.villcode;
ตรวจสอบการคัดกรองNCDในคนตายและTypeareaไม่ใช่ 1+3
==========================================================================
=======
select
person.pidaspid,
concat(ctitle.titlename,person.fname,' ',person.lname)aspname,
age_year,
person.idcardaspcid,
person.typeliveas'สถานะการอยู่อาศัย',
case whenperson.dischargetype ='1' then'ตาย'whenperson.dischargetype ='2'then
'ย้ายออกนอกเขต'whenperson.dischargetype='3'then'สุญหาย'
whenperson.dischargetype ='9'then'ยังไม่จาหน่าย'elsenull endas'สาเหตุจาหน่าย',
98. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
98
person.hnomoi ashno,
person.mumoi asmu,
DATE_FORMAT(screen_date,'%Y-%m-%d') asscreen_date
from
ncd_person_ncd_screeninnerjoinpersononncd_person_ncd_screen.pid=person.pid
innerjoinctitle onperson.prename=ctitle.titlecode
innerjoinncd_person onncd_person_ncd_screen.pcucode =ncd_person.pcucodeand
ncd_person_ncd_screen.pid=ncd_person.pid
where
age_year>= '15' and ncd_person_ncd_screen.screen_datebetween'2013-10-01'and '2014-
09-30' and (person.typelive notin('1','3') or person.dischargetype ='1')
groupby person.pid
orderby ncd_person.village;
ตรวจสอบการให้รหัสสมุนไพรที่ไม่ขึ้นต้นด้วย41และ42
==========================================================================
=======
selectdrugcode ,drugname,drugcode24,drugtype
fromcdrug
where drugtype ='10' anddrugflag= '1'
and cdrug.drugcode24notLike '41%'and cdrug.drugcode24notLike '42%';
ตรวจสอบการจาหน่ายตายในChronic
==========================================================================
=======
select
concat(ctitle.titlename,person.fname,'',person.lname,'','(',convert(person.pidusingutf8),')')as'ชื่อ-
สกุล',
person.idcardaspidcard,
person.hnomoias'บ้านเลขที่',
person.mumoi as'หมู่',
99. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
99
person.typelive as'สถานะการอยู่อาศัย',
case whenperson.dischargetype ='1' then'ตาย' whenperson.dischargetype='2'then
'ย้ายออกนอกเขต'whenperson.dischargetype='3'then'สุญหาย'
whenperson.dischargetype ='9'then'ยังไม่จาหน่าย'else nullendAS'สาเหตุจาหน่าย',
persondeath.deaddate as'วันที่ตาย',
personchronic.chroniccodeas'รหัสเรื้อรัง',
personchronic.typedischartas'สาเหตุจาหน่ายเรื้อรัง'
from
personinnerjoinctitle onperson.prename=ctitle.titlecode
innerjoinpersonchroniconperson.pcucodeperson =personchronic.pcucodeperson
and person.pid=personchronic.pid
innerjoinpersondeathonperson.pcucodeperson=persondeath.pcucodeperson
and person.pid=persondeath.pid
where personchronic.typedischart!='02';
====================================
รัน code ตรวจสอบว่าใครที่เราบันทึกวิธีการตรวจผิดใครที่พบว่าเป็นกลุ่มเสี่ยงแต่เราเอามาบันทึกก่อน***
SELECT n.pid,n.screen_date,n.bstest,n.bsl FROMncd_person_ncd_screenn
where n.screen_date between'2015-10-01'and'2016-09-30'and (bstest<> '3' or n.bsl between'100'
and '125')
====================================
รายชื่อกลุ่มเป้าหมายคัดกรองพัฒนาการเด็กปฐมวัยช่วงอายุ9,18,30,และ42เดือน
*** แก้ไขช่วงวันที่ในแต่ละเดือนที่รันcode โดยกาหนดเป็น'yyyy-mm-dd'*** ตัวหนังสือสีแดง
select
person.pidaspid,
concat(ctitle.titlename,person.fname,'',person.lname)aspname,
person.birthaspbirth,
GetAgeymd(person.birth,'2015-12-01')asage,
100. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
100
person.idcardasidcard,
person.hnomoiashno,
village.villnoasvillno,
case whenGetAgemonth(person.birth,'2015-12-01')='9'then"/"else '' endas '9เดือน',
case whenGetAgemonth(person.birth,'2015-12-01')='18'then"/"else '' endas '18เดือน',
case whenGetAgemonth(person.birth,'2015-12-01')='30'then"/"else '' endas '30เดือน',
case whenGetAgemonth(person.birth,'2015-12-01')='42'then"/"else '' endas '42เดือน'
frompersoninnerjoinctitle onperson.prename =ctitle.titlecode
innerjoinhouse onperson.pcucodeperson=house.pcucode andperson.hcode =house.hcode
innerjoinvillageonhouse.pcucode =village.pcucodeandvillage.villcode =house.villcode
where person.typelive in('1','3') andperson.pidnotin(selectpersondeath.pidfrom
persondeath)andsubstring(house.villcode,7,2)<>'00'
and GetAgemonth(person.birth,'2015-12-01') in('9','18','30','42')
orderby villno
==================================
reportผลการคัดกรองNCD59
เงื่อนไข
1. หากป่วยความดันผลการคัดกรองHTจะว่าง
2. หากป่วยเบาหวานผลการคัดกรองDMจะว่างแต่ถ้าคุณบันทึกค่าน้าตาลในผู้ป่วยเบาหวาน
ผลการคัดกรองจะออกมาเสมอ
3. ผลการคัดกรองDMจะออกก็ต่อเมื่อเลือกวิธีการตรวจเป็น3. DTX(อดอาหาร)
4. กลุ่มเป้าหมายอายุ35 ปีขึ้นไปทุกคนยกเว้นกลุ่มป่วยสองโรค
select
person.pidaspid,
concat(ctitle.titlename,person.fname,'',person.lname)aspname,
date_format(person.birth,'%d/%m/%Y') aspbirth,
GetAgeYearNum(person.birth,'2015-10-01')asage,
person.idcardasidcard,
101. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
101
person.hnomoiashno,
village.villnoasvillno,
case when(pcht.chroniccodeisnotnull andpcdm.chroniccode isnull )then'1'else '' endas 'ป่วยht',
case when(pcdm.chroniccode isnotnull and pcht.chroniccode isnull )then'1'else '' endas 'ป่วยdm',
n.screen_date,n.height,n.weight,n.waist,n.hbp_s1,n.hbp_d1,n.bsl,n.bmi,
case whenn.bsl <'100'and n.bstest= '3'then'ปกติ' whenn.bsl between'100'and'125' andn.bstest=
'3'then'เสี่ยง' when n.bsl >='126' andn.bstest= '3'then'สงสัยว่าป่วย'else ''endas 'ผลคัดDM',
case when(n.hbp_s1<'120' or n.hbp_d1<'80')and person.pidnotin(selectperson.pidfromperson
leftjoinpersonchronicpchtonperson.pid=pcht.pidandperson.pcucodeperson =
pcht.pcucodepersonandtrim(pcht.chroniccode) ='I10'
leftjoinpersonchronicpcdmonperson.pid=pcdm.pidandperson.pcucodeperson=
pcdm.pcucodepersonandtrim(pcdm.chroniccode)between'E10'and 'E15.9'
leftjoinpersonchronicpcon person.pid=pc.pidandperson.pcucodeperson=pc.pcucodeperson
where pcht.chroniccode isnotnull andpcdm.chroniccode isnull) then'ปกติ'
when((n.hbp_s1between'120'and'139') or (n.hbp_d1between'80'and'89')) andperson.pidnotin
(selectperson.pidfrompersonleftjoinpersonchronicpchtonperson.pid=pcht.pidand
person.pcucodeperson=pcht.pcucodepersonandtrim(pcht.chroniccode) ='I10'
leftjoinpersonchronicpcdmonperson.pid=pcdm.pidandperson.pcucodeperson=
pcdm.pcucodepersonandtrim(pcdm.chroniccode)between'E10'and 'E15.9'
leftjoinpersonchronicpcon person.pid=pc.pidandperson.pcucodeperson=pc.pcucodeperson
where pcht.chroniccode isnotnull andpcdm.chroniccode isnull)then'เสี่ยง'
when((n.hbp_s1between'140'and'179') or (n.hbp_d1between'90'and'109'))andperson.pidnotin
(selectperson.pidfrompersonleftjoinpersonchronicpchtonperson.pid=pcht.pidand
person.pcucodeperson=pcht.pcucodepersonandtrim(pcht.chroniccode) ='I10'
leftjoinpersonchronicpcdmonperson.pid =pcdm.pidandperson.pcucodeperson=
pcdm.pcucodepersonandtrim(pcdm.chroniccode)between'E10'and 'E15.9'
leftjoinpersonchronicpcon person.pid=pc.pidandperson.pcucodeperson=pc.pcucodeperson
where pcht.chroniccode isnotnull andpcdm.chroniccode isnull) then'สงสัยว่าป่วย'
when(n.hbp_s1>= '180' or n.hbp_d1>='110')andperson.pidnotin(selectperson.pidfromperson
leftjoinpersonchronicpchtonperson.pid=pcht.pidandperson.pcucodeperson=
pcht.pcucodepersonandtrim(pcht.chroniccode) ='I10'
leftjoinpersonchronicpcdmonperson.pid=pcdm.pidandperson.pcucodeperson=
pcdm.pcucodepersonandtrim(pcdm.chroniccode)between'E10'and 'E15.9'
102. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
102
leftjoinpersonchronicpcon person.pid=pc.pidandperson.pcucodeperson=pc.pcucodeperson
where pcht.chroniccode isnotnull andpcdm.chroniccode isnull) then'ฉุกเฉิน'else ''endas 'ผลคัดHT'
frompersoninnerjoinctitle onperson.prename =ctitle.titlecode
leftjoinpersonchronicpchtonperson.pid=pcht.pidandperson.pcucodeperson=
pcht.pcucodepersonandtrim(pcht.chroniccode) ='I10'
leftjoinpersonchronicpcdmonperson.pid=pcdm.pidandperson.pcucodeperson=
pcdm.pcucodepersonandtrim(pcdm.chroniccode)between'E10'and 'E15.9'
leftjoinpersonchronicpcon person.pid=pc.pidandperson.pcucodeperson=pc.pcucodeperson
innerjoinhouse onperson.pcucodeperson=house.pcucode andperson.hcode =house.hcode
innerjoinvillageonhouse.pcucode =village.pcucodeandvillage.villcode =house.villcode
innerjoinncd_person_ncd_screenn onperson.pid=n.pid
where person.typelive in('1','3') andperson.pidnotin(selectpersondeath.pidfrompersondeath)
and GetAgeYearNum(person.birth,'2015-10-01')>='35'and substring(house.villcode,7,2)<>'00'
and person.pidnotin(selectperson.pidfrompersonleftjoinpersonchronicpchtonperson.pid=
pcht.pidandperson.pcucodeperson=pcht.pcucodepersonandtrim(pcht.chroniccode) ='I10'
leftjoinpersonchronicpcdmonperson.pid=pcdm.pidandperson.pcucodeperson=
pcdm.pcucodepersonandtrim(pcdm.chroniccode)between'E10'and 'E15.9'
leftjoinpersonchronicpcon person.pid=pc.pidandperson.pcucodeperson=pc.pcucodeperson
where pcdm.chroniccode isnotnull andpcht.chroniccode isnotnull)
and n.screen_date between'2015-10-01'and'2016-09-30'
groupby person.pid
orderby villno,person.pid
====================================
ตรวจสอบการให้ ICD10ที่มี dxtype =01 มากกว่า1รหัส
SELECT visit.pid,v.visitno,COUNT(v.visitno) AS'จานวนซ้า',group_concat(v.diagcode) as'icdที่ให้รหัส01'
FROMvisitdiagvinnerjoinvisitonv.visitno=visit.visitno
where v.dxtype ='01' andvisit.visitdatebetween'2015-10-01'and'2016-09-30'
GROUP BY v.visitno
HAVING(COUNT(v.visitno) >1)
====================================
107. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
107
frompersonchronicwhere personchronic.typedischart='03')andperson.candobedhomesocial ='3'
then'1' else ''end) as 'NCDกลุ่ม3',
sum(case whengetAgeYearNum(person.birth,'2014-10-01')>=60then'1'else ''end) as 'Old',
sum(case whengetAgeYearNum(person.birth,'2014-10-01')>=60andperson.candobedhomesocial =
'1' then'1' else '' end) as 'Oldกลุ่ม1',
sum(case whengetAgeYearNum(person.birth,'2014-10-01')>=60andperson.candobedhomesocial =
'2' then'1' else '' end) as 'Oldกลุ่ม2',
sum(case whengetAgeYearNum(person.birth,'2014-10-01')>=60andperson.candobedhomesocial =
'3' then'1' else '' end) as 'Oldกลุ่ม3'
frompersoninnerjoinctitle onperson.prename =ctitle.titlecode
innerjoinhouse onperson.pcucodeperson=house.pcucode andperson.hcode =house.hcode
innerjoinvillageonhouse.pcucode =village.pcucodeandvillage.villcode =house.villcode
leftjoinuseronhouse.usernamedoc=user.username
where person.typelive IN ('1','3')andperson.pidnotin(selectpersondeath.pidfrom
persondeath)andsubstring(house.villcode,7,2)<>'00'
groupby village.villcode
)as orderp
code ดึงรายชื่อผู้สูงอายุผู้พิการผู้ป่วย NCDตามสภาพปัญหา
===========================================
select*from(selectperson.idcard,
concat(ctitle.titlename,person.fname,'',person.lname)as'ชื่อ-สกุล',
GetAgeYearNum(person.birth,current_date)as'อายุ',
house.hnoas'บ้านเลขที่',
right(house.villcode,2)as'หมู่ที่',
case whenconcat(person.pcucodeperson,person.pid)in(select
concat(personunable.pcucodeperson,personunable.pid)
frompersonunable ) then'/'else ''endas 'Disabl',
case whenconcat(person.pcucodeperson,person.pid) in(select
concat(personchronic.pcucodeperson,personchronic.pid)
frompersonchronicwhere personchronic.typedischart='03') then'/' else ''endas 'NCD',
108. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
108
case whengetAgeYearNum(person.birth,'2014-10-01')>=60then'/'else '' endas 'Old',
case whenperson.candobedhomesocial='1' then'/' else '' endas 'กลุ่ม1',
case whenperson.candobedhomesocial='2' then'/' else '' endas 'กลุ่ม2',
case whenperson.candobedhomesocial='3' then'/' else '' endas 'กลุ่ม3'
frompersoninnerjoinctitle onperson.prename =ctitle.titlecode
innerjoinhouse onperson.pcucodeperson=house.pcucode andperson.hcode =house.hcode
innerjoinvillageonhouse.pcucode =village.pcucodeandvillage.villcode =house.villcode
where person.typelive IN ('1','3')andperson.pidnotin(selectpersondeath.pidfrom
persondeath)andsubstring(house.villcode,7,2)<>'00'
groupby person.pid
orderby house.villcode)asorderp
where orderp.Disabl='/'or orderp.NCD='/'or orderp.old='/'
สรุปจานวนการคัดกรองแยกกลุ่มอายุให้เปลี่ยนวันเริ่มต้นและสิ้นสุดการตัดรายงานตัวหนังสือสีแดง
===================================================
SELECT
CASE WHEN hbp_s1 <'120' and hbp_d1<'80' and bsl < '100' THEN 'ปกติ'
WHEN hbp_s1 <'120' andhbp_d1 <'80' and bsl >= '100' THEN 'เสี่ยงDM'
WHEN (hbp_s1>= '120' or hbp_d1>= '80') and bsl < '100' THEN 'เสี่ยงHT'
WHEN (hbp_s1>= '120' or hbp_d1>= '80') and bsl >= '100' THEN 'เสี่ยงDM&HT' ELSE '' END AS
levelrisk,
CASE WHEN age_yearbetween'35'and'59' THEN '35-59ปี'
WHEN age_year>='60' THEN '>60ปี' ELSE '' END ASgage,
count(distinctncd_person_ncd_screen.pid) as ผลงาน
FROM
ncd_person_ncd_screenINNERJOIN personON ncd_person_ncd_screen.pid=person.pid
INNERJOIN ncd_person ON ncd_person_ncd_screen.pcucode =ncd_person.pcucodeAND
ncd_person_ncd_screen.pid=ncd_person.pid
WHERE age_year>= '35' and ncd_person_ncd_screen.screen_datebetween'2014-10-01'and'2015-
01-31' and concat(ncd_person_ncd_screen.pid,ncd_person_ncd_screen.pcucode) notin
110. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
110
DATE_FORMAT(screen_date,'%Y-%m-%d') asscreen_date,
max(case whenhbp_s1 <'120' and hbp_d1<'80' andbsl < '100' then"/" else '' end) as"ปกติ",
max(case whenhbp_s1 <'120' and hbp_d1<'80' andbsl >= '100' then"/"else '' end) as "เสี่ยงDM",
max(case when(hbp_s1>='120' or hbp_d1>= '80') and bsl < '100' then"/" else '' end) as "เสี่ยงHT",
max(case when(hbp_s1>='120' or hbp_d1>= '80') and bsl >= '100' then"/" else '' end) as
"เสี่ยงDM&HT"
from ncd_person_ncd_screeninnerjoin persononncd_person_ncd_screen.pid=person.pid
innerjoinctitle onperson.prename=ctitle.titlecode
innerjoinncd_person onncd_person_ncd_screen.pcucode =ncd_person.pcucodeand
ncd_person_ncd_screen.pid=ncd_person.pid
where
age_year>= '35' and ncd_person_ncd_screen.screen_datebetween'2014-10-01'and'2015-02-02'
and concat(ncd_person_ncd_screen.pid,ncd_person_ncd_screen.pcucode) notin
111. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
111
(selectconcat(ncd_person_ncd_hist.pid,ncd_person_ncd_hist.pcucode)
from ncd_person_ncd_hist) andSUBSTRING(ncd_person.village,7,2)<>'00'
groupby person.pid
orderby ncd_person.village
รายชื่อเด็ก0-60เดือน ชั่งน้าหนัก
SELECT person.pid
,ctitle.titlename
,person.fname
,person.lname
,person.hnomoi
,person.mumoi
,getAgeYearNum(person.birth,CURDATE()) AS นับอายุเป็นปี
,getagemonth(person.birth,CURDATE())AS นับอายุเป็นเดือน
FROMperson
LEFT JOIN persondeathON persondeath.pcucodeperson=person.pcucodepersonAND
persondeath.pid=person.pid
INNERJOIN house ON person.pcucodeperson=house.pcucode ANDperson.hcode =house.hcode
INNERJOIN ctitle ON ctitle.titlecode =person.prename
WHERE persondeath.pidISNULL
ANDgetagemonth(person.birth,CURDATE()) <=60
ANDperson.typelive IN ('0','1','3')
ORDER BY house.villcode,person.hnomoi*1;
ผลงานการคัดกรองปี 58แยกรายหมู่
112. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
112
selectvillage.villnoas หมู่,village.villname as ชื่อหมู่บ้าน,count(distinctperson.pid) as เป้า,
count(distinctsc.pid) as ผลงาน,concat(round(count(distinctsc.pid)*100/count(distinct
person.pid),2),'%') as เปอรเซ็น ,count(distinctcase whengetAgeYearNum(birth,'20141001') between
35 and 59 thenperson.pidelse nullend) as"เป้า35-59",count(distinctcase when
getAgeYearNum(birth,'20141001') between 35and59 andsc.pidis notnull thenperson.pidelse null
end) as ผลงาน,concat(round(count(distinctcase whengetAgeYearNum(birth,'20141001') between 35
and 59 andsc.pidis notnull thenperson.pidelse nullend)*100/count(distinctcase when
getAgeYearNum(birth,'20141001') between 35and59 thenperson.pidelse nullend),2),'%') as"%35-
59",count(distinctcase whengetAgeYearNum(birth,'20141001') >=60 thenperson.pidelse null end)
as "เป้า>= 60" ,count(distinctcase whengetAgeYearNum(birth,'20141001') >=60and sc.pidisnot null
thenperson.pidelse null end) as ผลงาน,concat(round(count(distinctcase when
getAgeYearNum(birth,'20141001') >=60 and sc.pidisnotnull thenperson.pidelse nullend)*100/
count(distinctcase whengetAgeYearNum(birth,'20141001') >=60 thenperson.pidelse nullend),2),'
%') as "%>= 60"from personleftjoinncdpersonscreenall sconperson.pid=sc.pidand
person.pcucodeperson=sc.pcucode andsc.screen_date between'20141001'and'20150930'inner
joinhouse onperson.hcode =house.hcodeandperson.pcucodeperson=house.pcucode innerjoin
village onhouse.villcode=village.villcode andvillno<>'00' where
getAgeYearNum(birth,'20141001')>='35'and person.typelivein('0','1','3') andperson.dischargetype
<> 1 and person.pidnotin (selectpidfrompersonchronicwhere (personchronic.chroniccode='I10'or
personchronic.chroniccodebetween'E10'and 'E15')) group byvillno,villname unionselect'' as
หมู่,'รวม' as ชื่อหมู่บ้าน,count(distinctperson.pid) as เป้า,count(distinctsc.pid)as
ผลงาน,concat(round(count(distinctsc.pid)*100/count(distinctperson.pid),2),'%') as เปอรเซ็น
,count(distinctcase whengetAgeYearNum(birth,'20141001') between 35and59thenperson.pidelse
null end) as"เป้า35-59",count(distinctcase whengetAgeYearNum(birth,'20141001') between 35and
59 and sc.pidisnot null thenperson.pidelsenull end) as ผลงาน,concat(round(count(distinctcase
whengetAgeYearNum(birth,'20141001') between 35and59 and sc.pidisnot null thenperson.pid
else null end)*100/count(distinctcase whengetAgeYearNum(birth,'20141001') between 35and59
thenperson.pidelse null end),2),'%') as"%35-59",count(distinctcase when
getAgeYearNum(birth,'20141001') >=60 thenperson.pidelse nullend) as"เป้า>=60" ,count(distinct
case whengetAgeYearNum(birth,'20141001') >=60 and sc.pidisnot null thenperson.pidelsenull
end) as ผลงาน,concat(round(count(distinctcase whengetAgeYearNum(birth,'20141001') >=60 and
sc.pidisnot null thenperson.pidelsenull end)*100/count(distinctcase when
getAgeYearNum(birth,'20141001') >=60 thenperson.pidelse nullend),2),'%') as"%>= 60" from
personleftjoinncdpersonscreenall sconperson.pid=sc.pidandperson.pcucodeperson=
sc.pcucode andsc.screen_date between'20141001'and'20150930'innerjoinhouse onperson.hcode
113. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
113
= house.hcode andperson.pcucodeperson=house.pcucode innerjoinvillageonhouse.villcode=
village.villcode andvillno<>'00' where getAgeYearNum(birth,'20141001')>=35and person.typelive
in('1','3') andperson.dischargetype<>1 and person.pidnotin(selectpidfrompersonchronicwhere
(personchronic.chroniccode='I10'orpersonchronic.chroniccodebetween'E10'and'E15')) group by
หมู่, ชื่อหมู่บ้าน;
กลุ่มเป้าหมายการคัดกรองต้อกระจกมากกว่า60ปี
SELECT
person.pid ASpid,
concat(ctitle.titlename,person.fname,'',person.lname)aspname,
person.birthASpbirth,
GetAgeYearNum(person.birth,CURRENT_DATE)asage,
person.idcardASpcid,
person.sex ASsex,
person.hnomoiAShno,
village.villnoASvillage_villno,
village.villnameASvillage_villname
FROM
personINNERJOIN ctitle ON person.prename =ctitle.titlecode
INNERJOIN house ON person.pcucodeperson=house.pcucode
ANDperson.hcode =house.hcode
INNERJOIN village ON house.pcucode =village.pcucode
ANDvillage.villcode =house.villcode
WHERE
person.typelive IN ('1','3')
and person.pidnotin(SELECTpersondeath.pidFROMpersondeath)
and GetAgeYearNum(person.birth,'2014-10-01')>='60'
and SUBSTRING(house.villcode,7,2)<>'00'
orderBY village.villno,person.hnomoi*1;
โค้ดรายชื่อผู้ป่วยความดันเบาหวานที่มารับบริการพร้อมค่าความดันและน้าตาลในเลือด
select
concat(ctitle.titlename,person.fname,'',person.lname)as'ชื่อ-สกุล',
114. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
114
GetAgeYearNum(person.birth,CURRENT_DATE)as'อายุ',
concat(person.hnomoi,'','หมู่',person.mumoi) as'ที่อยู่',
case when(pcht.chroniccodeisnotnull andpcdm.chroniccode isnull )then'1'else '' endas ht
,case when(pcdm.chroniccode isnotnull andpcht.chroniccode isnull) then'1' else '' endas dm
,case whenpcdm.chroniccode isnotnull andpcht.chroniccode isnotnull then'1'else '' endas htdm,
max(case whenmonth(visit.visitdate)=10thenvisit.pressure else ''end) as'ต.ค.',
max(case whenmonth(visit.visitdate)=11thenvisit.pressure else ''end) as'พ.ย.',
max(case whenmonth(visit.visitdate)=12thenvisit.pressure else ''end) as'ธ.ค.',
max(case whenmonth(visit.visitdate)=1thenvisit.pressure else''end) as 'ม.ค.',
max(case whenmonth(visit.visitdate)=2thenvisit.pressure else''end) as 'ก.พ.',
max(case whenmonth(visit.visitdate)=3thenvisit.pressure else''end) as 'มี.ค.',
max(case whenmonth(visit.visitdate)=4thenvisit.pressure else''end) as 'เม.ย.',
max(case whenmonth(visit.visitdate)=5thenvisit.pressure else''end) as 'พ.ค.',
max(case whenmonth(visit.visitdate)=6thenvisit.pressure else''end) as 'มิ.ย.',
max(case whenmonth(visit.visitdate)=7thenvisit.pressure else''end) as 'ก.ค.',
max(case whenmonth(visit.visitdate)=8thenvisit.pressure else''end) as 'ส.ค.',
max(case whenmonth(visit.visitdate)=9thenvisit.pressure else''end) as 'ก.ย.'
fromperson
innerjoinctitle onperson.prename =ctitle.titlecode
innerjoinhouse onperson.hcode=house.hcode andperson.pcucodeperson=house.pcucode
leftjoinvisitON person.pcucodeperson=visit.pcucodepersonand person.pid=visit.pid
leftjoinvisitdiagonvisit.visitno=visitdiag.visitno
innerjoinvillageonhouse.villcode =village.villcodeandhouse.pcucode =village.pcucode
LEFT JOIN personchronicpchtonperson.pid=pcht.pidandperson.pcucodeperson=
pcht.pcucodepersonandtrim(pcht.chroniccode) ='I10'
LEFT JOIN personchronicpcdmonperson.pid=pcdm.pidandperson.pcucodeperson=
pcdm.pcucodepersonandtrim(pcdm.chroniccode)between'E10'and 'E15.9'
LEFT JOIN personchronicpconperson.pid=pc.pidandperson.pcucodeperson=pc.pcucodeperson
WHERE visit.visitdate between'2014-10-01'and'2015-09-30'
115. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
115
and SUBSTRING(house.villcode,7,2)<>'00'and (visitdiag.diagcode between'E10'and'E15.9' or
(visitdiag.diagcode='I10'))
and pc.cup= pc.pcucodeperson
GROUP BY pc.pid
orderBY village.villno
โค้ด ประชากรจาแนกตามกลุ่มวัย
selectvillage.villnoasvillno,village.villname asvillname
,count(person.pid) as'total'
,sum(case whenGetAgeYearNum(person.birth,'2014-10-01') <5 then1 else '' end) as'0-5ปี'
,sum(case whenGetAgeYearNum(person.birth,'2014-10-01') between 5and14 then1 else '' end) as
'5-14ปี'
,sum(case whenGetAgeYearNum(person.birth,'2014-10-01') between 15and21 then1 else ''end) as
'15-21ปี'
,sum(case whenGetAgeYearNum(person.birth,'2014-10-01') between 15and59 then1 else ''end) as
'15-59ปี'
,sum(case whenGetAgeYearNum(person.birth,'2014-10-01') >=60 then1 else '' end) as'60ปี'
,sum(case whenconcat(person.pcucodeperson,person.pid)in(select
concat(personunable.pcucodeperson,personunable.pid)frompersonunable ) then 1else '' end) as
'พิการ'
frompersoninnerjoinhouse onperson.pcucodeperson=house.pcucode andperson.hcode =
house.hcode
innerjoinvillageonhouse.villcode=village.villcode
where concat(person.pid,person.pcucodeperson)notin(select
concat(persondeath.pid,persondeath.pcucodeperson)
frompersondeathwhere deaddate<=curdate()ordeaddate isnotnull)
and SUBSTRING(house.villcode,7,2)<>'00'andperson.typelivein('1','3')
groupby villno,villname
โค้ดรายชื่อประชากรอายุ20-50ปี เป้าหมายการฉีดวัคซีนdt(เกิดตั้งแต่1ม.ค.08-31 ธ.ค.38)
SELECT
person.pidASpid,
concat(ctitle.titlename,person.fname,' ',person.lname)aspname,
116. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
116
person.birthASpbirth,
GetAgeYearNum(person.birth,CURRENT_DATE)asage,
person.idcardASpcid,
person.sex ASsex,
person.hnomoiAShno,
person.mumoi ASmu,
village.villnoASvillage_villno,
village.villnameASvillage_villname
FROM
personINNERJOIN ctitle ON person.prename =ctitle.titlecode
INNERJOIN house ON person.pcucodeperson=house.pcucode
ANDperson.hcode =house.hcode
INNERJOIN village ON house.pcucode =village.pcucode
ANDvillage.villcode =house.villcode
WHERE
person.typelive IN ('1','3')
and person.pidnotin(SELECTpersondeath.pidFROMpersondeath)
and person.birth between'1965-01-01'and'1995-12-31'
and SUBSTRING(house.villcode,7,2)<>'00'
ANDnation='99'
orderBY village.villno,person.hnomoi*1;
โค้ดรายชื่อผู้ป่วยNCDระบุสถานที่รักษา
selectp.pid
,concat(ctitle.titlename
,' ',p.fname,'',p.lname) AS'name'
,YEAR(FROM_DAYS(DATEDIFF(CURDATE(),p.birth))) as'age'
,p.idcardas CID
,house.hnoas'address'
,village.villnoAS'moo'
,GROUP_CONCAT(pc.chroniccode) as chroniccode
,case when(pcht.chroniccode isnotnull andpcdm.chroniccode isnull)then'1'else ''endas ht
,case when(pcdm.chroniccode isnotnull andpcht.chroniccode isnull) then'1' else '' endas dm
117. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
117
,case whenpcdm.chroniccode isnotnull andpcht.chroniccode isnotnull then'1'else '' endas htdm
,DATE_FORMAT(pcdm.datefirstdiag,'%Y-%m-%d') as'dx_first_DM'
,DATE_FORMAT(pcht.datefirstdiag,'%Y-%m-%d') as'dx_first_HT'
,pc.cupas 'hospital'
,pc.hospfirstdiagas'first_dx_hosp'
frompersonp
INNERJOIN house ON p.hcode = house.hcode ANDp.pcucodeperson=house.pcucode
INNERJOIN village ON house.villcode =village.villcode ANDhouse.pcucode =village.pcucode
LEFT JOIN ctitle ON p.prename =ctitle.titlecode
LEFT JOIN personchronicpchtonp.pid= pcht.pidandp.pcucodeperson=pcht.pcucodepersonand
trim(pcht.chroniccode) ='I10'
LEFT JOIN personchronicpcdmonp.pid= pcdm.pidandp.pcucodeperson=pcdm.pcucodeperson
and trim(pcdm.chroniccode) between'E10'and 'E15.9'
LEFT JOIN personchronicpconp.pid= pc.pidand p.pcucodeperson=pc.pcucodeperson
WHERE (pcdm.chroniccode
between'E10'and'E15.9' or pcht.chroniccode ='I10')
and right(village.villcode,2)!='00'
and p.typelive in(1,3)andpc.typedischart='03'
ANDconcat(p.pid,p.pcucodeperson)NOT IN (select
concat(persondeath.pid,persondeath.pcucodeperson)
frompersondeathwhere deaddate <=CURDATE() or deaddate isnull)
GROUP BY p.pid,ht,dmORDERBYvillage.villcode*1
โค้ดเป้าหมายงานทันตกรรม
SELECT
SUM(CASE WHEN GetAgeYearNum(person.birth,'2014-10-01') BETWEEN 0AND 2 THEN 1 ELSE null
END) AS m02,
SUM(CASE WHEN GetAgeYearNum(person.birth,'2014-10-01') BETWEEN 3AND 5 THEN 1 ELSE null
END) AS m35,
SUM(CASE WHEN GetAgeYearNum(person.birth,'2014-10-01') BETWEEN 6AND 14THEN 1 ELSE null
END) AS m614,
SUM(CASE WHEN GetAgeYearNum(person.birth,'2014-10-01') >=60THEN 1 ELSE null END) AS m60
118. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
118
FROM
person
LeftOuterJoinhouse ON person.hcode =house.hcode ANDhouse.pcucode =person.pcucodeperson
LeftOuterJoinvillage ON house.villcode =village.villcode ANDvillage.pcucode =house.pcucode
LeftOuterJoincright ON person.rightcode=cright.rightcode
WHERE SUBSTRING(house.villcode,7,2)<>'00'andperson.typelivein('1','3')
and CONCAT(person.pid,person.pcucodeperson) NOTIN (select
CONCAT(persondeath.pid,persondeath.pcucodeperson)frompersondeath)
ตรวจสอบคนที่คัดกรองNCDซ้า
selectcount(n.pid),pid
fromncd_person_ncd_screenn
where screen_date between'2014-10-01'and'2015-09-30'
groupby n.pid
havingcount(n.pid)>1
เป้าหมายการรณรงค์ฉีดวัคซีนDt58
selectvillage.villnoasvillno,village.villname asvillname,
count(distinctp.pid) aspop,
count(distinctcase whenvisitepi.vaccinecode ='Dtc' thenvisitepi.pidelse null end) as ผลงาน,
concat(round(count(distinctcase whengetAgeYearNum(p.birth,current_date)between'20'and'50'
and visitepi.vaccinecode ='Dtc' thenp.pidelse null end)*100/count(distinctcase when
getAgeYearNum(p.birth,current_date)between'20'and '50' thenp.pidelse null end),2),'%') as
'ร้อยละ'
frompersonp
innerjoinhouse onp.hcode =house.hcode andp.pcucodeperson=house.pcucode
innerjoinvillageonhouse.villcode=village.villcode andvillno<>'00'
leftjoinvisitepi onp.pcucodeperson=visitepi.pcucodepersonandp.pid=visitepi.pid
and dateepi between'2014-10-01'and'2015-09-30'
where getAgeYearNum(p.birth,current_date)between'20'and'50' and p.typelive in('1','3')
groupby villno,villname
119. Basic MySQL
ศูนย์เทคโนโลยีสารสนเทศและการสื่อสาร สานักงานปลัดกระทรวงสาธารณสุข
119
เป้าหมายการคัดกรองปี 58ตัดกลุ่มป่วย
select
(select
count(p.pid)
from
personp
innerjoinctitle onp.prename =ctitle.titlecode
innerjoinhouse onp.pcucodeperson=house.pcucodeandp.hcode =house.hcode
where CONCAT(p.pid,p.pcucodeperson) NOTIN (select
CONCAT(persondeath.pid,persondeath.pcucodeperson)frompersondeath)
and SUBSTRING(house.villcode,7,2)<>'00' andp.typelivein('1','3') and
GetAgeYearNum(p.birth,'2014-10-01')>='35'
and p.pidnotin(selectpidfrompersonchronicwhere(personchronic.chroniccode='I10'or
personchronic.chroniccodebetween'E10'and 'E15.9'))) as 'ตัดกลุ่มป่วย',
(select
count(p.pid)
from
personp
innerjoinctitle onp.prename =ctitle.titlecode
innerjoinhouse onp.pcucodeperson=house.pcucodeandp.hcode =house.hcode
where CONCAT(p.pid,p.pcucodeperson) NOTIN (select
CONCAT(persondeath.pid,persondeath.pcucodeperson)frompersondeath)
and SUBSTRING(house.villcode,7,2)<>'00' andp.typelivein('1','3') and
GetAgeYearNum(p.birth,'2014-10-01')>='35') as'ไม่ตัดกลุ่มป่วย'
รายชื่อกลุ่มป่วยแยกรายหมู่(ตัวหนังสือสีแดงให้เปลี่ยนเป็นวดป.ที่ต้องการเรียกรายงาน)
select
concat(ctitle.titlename,ps.fname,'',ps.lname)aspname,
ps.birthasbirth,
ps.sex assex,
ps.idcardas idcard,
ps.hnomoi,