2012-04-07 56 views
1

相關數據我有我的MySQL數據庫,它看起來像以下三個表: enter image description here找出從表

編輯

表「成員」擁有的名稱和一些成員的代碼。現在表格「老闆」包含被選爲事件老闆的成員的名字。每個老闆至少有一個下屬(請檢查下屬表)。

現在我想要做的是找出名稱和他們的點Bon Jovi(請檢查表格表格:成員)是與。當發現有條件時...如果來自table:boss的「condition_type」爲1,則來自下屬的值將位於右側,並且如果來自table:boss的「condition_type」爲0,則來自下屬的值將會在左側。

現在我正在試圖找出下面的樣子。

enter image description here

+0

看你這個帖子這會回答你 http://stackoverflow.com/questions/10053670/how-to-inter-link-three-mysql-tables-using-codeigniter/10053734# 10053734 – 2012-04-07 11:31:35

+0

感謝Raheel,您的回覆以及您在我另一篇文章中的回答。事情就是你在那篇文章中給我看的腳本..正確地加入這些表格,但不幸的是它並沒有幫助我實現我在這篇文章中提到的內容。你能幫我在這裏嗎? :) – 2012-04-07 12:10:14

+0

請讓您的問題更清楚 – 2012-04-07 13:02:26

回答

2

確定我已經找到你的解決方案我張貼

$condition = 1;// Condition will be provided by you for selection. Could be zero 
$data = array(
     'members.name', 
     'subordinates.points', 
     'subordinates.event' 
); 

$this->db->select($data); 
$this->db->join('members','members.code = subordinates.member_code','left'); 
$this->db->join('boss','boss.event = subordinates.event','left'); 
$this->db->where('boss.condition_type',$condition); 
$this->db->where('boss.event = subordinates.event'); 
$this->db->get('subordinates'); 

我也有在評選活動,讓你認識到事件的查詢將帶來的差異。請測試一下,看看它是否適合你。列名可能有一點區別。一個好的方法是像這樣使用表格之間的簡單易懂的關係。

Table1 : members 
Columns : id , code , name 
Table2 : boss 
Columns : id , event , member_code , condition_type 
Table3 : subordinates 
Columns : id , boss_event , member_code , points 
+0

你檢查它的工作好嗎? – 2012-04-10 13:53:23

+0

我很遺憾遲到的迴應。我之前無法檢查您的腳本,因爲我以某種方式丟失了所有數據庫。但今天我重新創建了數據庫並檢查了它,發現如果我更改$ condition = 1;到$ condition = 0;只有那麼它的作品。再次感謝您的回覆和時間。 :) – 2012-04-16 09:05:13

+0

我認爲你的waork已經被簡化了。你只需提供條件即可。 – 2012-04-16 09:42:18