การ Join ข้อมูลด้วย Codeigniter

การใช้งานฐานข้อมูลกับ CI



การ-Join-ข้อมูลด้วย-Codeigniter

คำสั่งการ Join ข้อมูล

$this->db->join()

ส่วนของการแสดงผล Select มาทั้งหมด

$this->db->select('*');

เลือกฐานข้อมูล blogs

$this->db->from('blogs');

คำสั่ง Join ระหว่าง table comments กับ table blogs โดยที่เงื่อนไขการ Join คือ ID ของ comments table กับ blogs table จะต้องมีค่าตรงกัน 

$this->db->join('comments', 'comments.id = blogs.id');

$query = $this->db->get();

คำสั่งจะเทียบเท่ากับ

// Produces:

// SELECT * FROM blogs JOIN comments ON comments.id = blogs.id

Multiple function calls can be made if you need several joins in one query.

สำหรับเงื่อนไขเมื่อต้องการ Left Join

$this->db->join('comments', 'comments.id = blogs.id', 'left');

// Produces: LEFT JOIN comments ON comments.id = blogs.id

เห็นไหมครับว่าการใช้งาน Join จาก codeigniter นั้นไม่ยากเลย


By : คุณากร วงษ์รวยดี

โปรไฟล์ Blog โปรไฟล์ E-portfolio

หมวดหมู่ : เทคโนโลยี

ผู้เข้าชม 1647 ครั้ง

Comment แสดงความคิดเห็น