开发者

Which join for 1: Many relationship?

Here is my Database schema and the tables I am trying to link, I tried using a LEFT join but now it is creating a new result for each matched row.

http://i.imgur.com/Ovsau.png

I am currently building up my query and now trying to join these tables together (Procedure-> Procedure_event -> Resources -> Med_item)

In a scenario like this:

e.g. Procedure X has 3 rows procedure_events

开发者_如何学Go

I want the data returned to me in one record but with all the rows that make up that record.

However it is returning 3 rows of results like so:

  • Procedure X - procedure_event row 1
  • Procedure X - procedure_event row 2
  • Procedure X - procedure_event row 3

So the query result should be Procedure X - procedureevent rows 1,2,3

This is my code in Codeigniter PHP framework using Active Record:

$this->db->join('procedure_event', 'procedure.procedure_id = procedure_event.procedure_id' , 'left');

Thanks


This is how LEFT JOIN works.

So the query result should be Procedure X - procedureevent rows 1,2,3

I think you need this: How do I get a count of associated rows in a left join in MySQL?

MySQL can't return two dimensional arrays, or merge rows. If you need all the "procedureevent rows 1,2,3" then just iterate through the result set, like you would normally do and add a small + icon that gets the procedureevents with AJAX.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜