CodeIgniter从两个以上的表中获取记录

时间:2013-07-16 10:55:24

标签: mysql sql codeigniter join codeigniter-2

我正在开发一个应用程序,我已经定义了以下表格。

storytags
id  cover_title                   user_id
1  love happens two times?         1
2  revolution 2020                 2
3  wings of fire                   3
4  night at the call centre        4

storytag_invitations
id  storytag_id  user_id
1      1            1
2      2            2
3      3            3
4      4            4
users
id  name
1  suhas
2  sangu
3  praveen
4  sangamesh

我想为用户3获取storytag_invitations.user_id!= storytags.user_id和storytag_invitations.storytag_id!= storytags.id的故事标题

我尝试了以下查询

select storytags.cover_title
from storytag_invitations
join storytags
on storytags.id != storytag_invitations.storytag_id and storytags.user_id !=  storytag_invitations.user_id
where storytag_invitations.user_id = 3

但是我得到了重复的行。请提出一些解决方案。已经两天了,我正在尝试这个。这项工作将更受欢迎。

3 个答案:

答案 0 :(得分:2)

//if you are using codeigniter then try this
$this->db->select("table1.column1,table1.column2,table2.column");
$this->db->join("table2","table1.column = table2.column");
$resultset=$this->db->get();

答案 1 :(得分:1)

尝试它是否适合您:

$sql    = "select storytags.cover_title from storytags, storytag_invitations where ( storytags.id != storytag_invitations.storytag_id and storytags.user_id !=  storytag_invitations.user_id ) and storytag_invitations.user_id = 3";
$rs     = $this->db->query($sql);

答案 2 :(得分:1)

当我使用时,你的sql对我有用:

select s.cover_title
  from storytag_invitations si, storytags s
 where s.id != si.storytag_id
   and s.user_id != si.user_id
   and si.user_id = 3

您可以在此处查看:http://sqlfiddle.com/#!4/ecd77/4