แสดงบทความที่มีป้ายกำกับ mysql แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ mysql แสดงบทความทั้งหมด

วันอังคารที่ 18 ตุลาคม พ.ศ. 2559

การใช้คำสั่ง join แสดงข้อมูล จาก 2 ฐานข้อมูล สำหรับ mysql


การใช้คำสั่ง join ของภาษา sql เพื่อแสดงข้อมูลนั้น ถือว่าเป็นคำสั่งที่ช่วยให้นักพัฒนาระบบสามารถเรียบเรียงข้อมูล
จากหลาย ๆ ตารางที่มีความสัมพันธ์กันมาแสดงร่วมกันได้อย่างลงตัวและมีประสิทธิภาพ และช่วยลดการใช้ select ข้อมูล
มาแสดงอย่างซ้ำซ้อนลงไปได้ ปัญหาที่เกิดขึ้นและมีความจำเป็นที่ต้องใช้การ join ข้อมูลจากหลาย ๆ ตารางนั้น
ก็มีมากมายแตกต่างกันไป แต่ปัญหาหนึ่งที่ผู้เขียนจะยกเป็นตัวอย่างในกรณีนี้คือ
การแสดงชื่อพนักงานและนามสกุลพนักงานจากแหล่งฐานข้อมูล 2 แหล่ง
โดยฐานข้อมูลแรกจะเก็บรายชื่อของพนักงาน และฐานข้อมูลตัวที่ 2 จะเก็บนามสกุลของพนักงานเอาไว้
แต่ต้องการทำรายงานที่ต้องแสดงทั้งชื่อและนามสกุลของพนักงานออกมา
อาจจะต้องใช้การ join ข้อมูลจาก 2 ฐานข้อมูลมาช่วยดังนี้
ฐานข้อมูลที่ 1
CREATE DATABASE `db1`
มี 1 ตาราง
CREATE TABLE `tb1` (
`id`  int(1) NULL ,
`name`  varchar(255) NULL ,
PRIMARY KEY (`id`)
)
;
ฐานข้อมูลที่ 2
CREATE DATABASE `db2`
มี 1 ตาราง
CREATE TABLE `tb2` (
`id`  int(1) NULL ,
`surname`  varchar(255) NULL ,
PRIMARY KEY (`id`)
)
;
โดยตาราง tb1 บน db1 และ tb2 บน db2 มีฟิลด์ id เป็นคีย์สำหรับเชื่อมโยงความสัมพันธ์กัน
แสดงข้อมูลใน tb1

แสดงข้อมูลใน tb2

ใช้การแสดงข้อมูลแบบ join ข้อมูลจาก 2 ฐานข้อมูล
SELECT
db1.tb1.`name`,
db2.tb2.surname
FROM
db1.tb1
INNER JOIN db2.tb2 ON db1.tb1.id = db2.tb2.id
แสดงผลข้อมูล

วันจันทร์ที่ 11 มกราคม พ.ศ. 2559

คำสั่งตรวจสอบรายงานการคัดกรอง และโรค ปี 2558

คำสั่งตรวจสอบรายงานการคัดกรอง และโรค ปี 2558

ขั้นตอนการดูรายงาน ให้ก๊อปคำสั่งไปวางใน sql query แล้วคลิก run


รายชื่อประชากร ที่อาจกรอกช้ำ ในงานคัดกรอง และ โรค ปี 2558
SELECT person.person_id,CONCAT(person.pname, person.fname," ",person.lname) AS `name`,
person.cid,ovstdiag.hn,ovstdiag.icd10,ovstdiag.vstdate,count(ovstdiag.hn)as ovstdiag2
FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z133" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY person.person_id HAVING ovstdiag2 >= 2
union
SELECT person.person_id,CONCAT(person.pname, person.fname," ",person.lname) AS `name`,
person.cid,ovstdiag.hn,ovstdiag.icd10,ovstdiag.vstdate,count(ovstdiag.hn)as ovstdiag2
FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z123" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY person.person_id HAVING ovstdiag2 >= 2
union
SELECT person.person_id,CONCAT(person.pname, person.fname," ",person.lname) AS `name`,
person.cid,ovstdiag.hn,ovstdiag.icd10,ovstdiag.vstdate,count(ovstdiag.hn)as ovstdiag2
FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z014" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY person.person_id HAVING ovstdiag2 >= 2
union
SELECT person.person_id,CONCAT(person.pname, person.fname," ",person.lname) AS `name`,
person.cid,ovstdiag.hn,ovstdiag.icd10,ovstdiag.vstdate,count(ovstdiag.hn)as ovstdiag2
FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z124" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY person.person_id HAVING ovstdiag2 >= 2
union
SELECT person.person_id,CONCAT(person.pname, person.fname," ",person.lname) AS `name`,
person.cid,ovstdiag.hn,ovstdiag.icd10,ovstdiag.vstdate,count(ovstdiag.hn)as ovstdiag2
FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z131" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY person.person_id HAVING ovstdiag2 >= 2
union
SELECT person.person_id,CONCAT(person.pname, person.fname," ",person.lname) AS `name`,
person.cid,ovstdiag.hn,ovstdiag.icd10,ovstdiag.vstdate,count(ovstdiag.hn)as ovstdiag2
FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z138" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY person.person_id HAVING ovstdiag2 >= 2

ผลงานโรค และการคัดกรอง ปี 2558

SELECT ovstdiag.icd10, count(person.sex)as total FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z001" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY ovstdiag.icd10
union
SELECT ovstdiag.icd10, count(person.sex)as total FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z012" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY ovstdiag.icd10
union
SELECT ovstdiag.icd10, count(person.sex)as total FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z108" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY ovstdiag.icd10
union
SELECT ovstdiag.icd10, count(person.sex)as total FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z133" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY ovstdiag.icd10
union
SELECT ovstdiag.icd10, count(person.sex)as total FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z123" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY ovstdiag.icd10
union
SELECT ovstdiag.icd10, count(person.sex)as total FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z014" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY ovstdiag.icd10
union
SELECT ovstdiag.icd10, count(person.sex)as total FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z124" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY ovstdiag.icd10
union
SELECT ovstdiag.icd10, count(person.sex)as total FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z131" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY ovstdiag .icd10
union
SELECT ovstdiag.icd10, count(person.sex)as total FROM ovstdiag INNER JOIN person ON person.patient_hn = ovstdiag.hn
WHERE ovstdiag.icd10 LIKE "Z138" AND ovstdiag.vstdate BETWEEN "2014-10-01" and "2015-06-30"
GROUP BY ovstdiag.icd10

cr.ชีวิต ต้องสู้

วันอาทิตย์ที่ 10 มกราคม พ.ศ. 2559

ลืม password root mysql

ลืม password root mysql
ขั้นตอนแรก หยุดการทำงานของ service mysql ใน linux ใช้

service mysql stop


จากนั้น run คำสั่ง

mysqld_safe --skip-grant-tables &


จากนั้น เรา login เข้า mysql root โดยไม่มี password ดังนี้

mysql -uroot mysql

ที่ช่องคำสั่งให้พิมพ์

UPDATE user SET password=PASSWORD("abcd") WHERE user="root";

และ FLUSH PRIVILEGES;

ตอนนี้ root password ของ mysql จะถูกเปลี่ยนเป็น abcd ซึ่งเราสามารถไปแก้ไขได้ต่อไป