2014-11-21 54 views
-2

你能帮我吗我有两张桌子imagesnote。我想从这些表中获取所有项目并按日期排序。有可能我在codeigniter中使用活动记录。表是独立的。从两张桌子的结果按日期排序

谢谢你的回复。

+0

我试过,但它让我错误操作 '联盟''SELECT *排序规则的非法混用( (SELECT images.link,images.date从图像中) UNION ALL (SELECT note.name_slovak,note.date FROM note) )results ORDER BY date DESC' – PatrikD 2014-11-21 09:12:14

回答

1

这应该工作:

$this->db->orderby("date", "ASC"); //or desc  
$query = $this->db->get('images'); //or $query = $this->db->get('note'); 
$result=$query->result_array(); 
print_r($result); 

,或者如果你想使用UNION ALL

$this->db->query('SELECT * FROM (SELECT id, date FROM images UNION ALL SELECT id, date FROM note) result ORDER BY result.date'); 

注意,在联盟内每个SELECT语句必须具有相同的列数。这些列也必须具有相似的数据类型。另外,每个SELECT语句中的列必须与http://www.w3schools.com/sql/sql_union.asp的顺序相同。

+0

同时从两张表中选择 – PatrikD 2014-11-21 09:09:00

+0

编辑我的答案。 – stuzzo 2014-11-21 09:30:27

0

你在哪张表中有日期栏?我假设你在图像表中有它。

$this->db->select('images.coulmn1, images.culmn2, images.date_col, note.col1, note.col2'); $this->db->from('images'); $this->db->join('note', 'note.key1 = images.key1); //key1 is key field to relate both table. $this->db->order_by("images.date_col", "desc"); $result = $this->db->get()->result_array();

希望这将有助于。

+0

他说表格不相关。 – stuzzo 2014-11-21 09:07:12

+0

我说表是独立的我不能使用连接和日期列都有。 – PatrikD 2014-11-21 09:08:29

+0

啊,对不起。我现在注意到了。 – 2014-11-21 09:22:15

0

试试这个:

$this->db->select('*'); 
$this->db->from('images'); 
$this->db->join('note'); 
$this->db->orderby("date column name", "ASC"); 
$query = $this->db->get(); 
$result=$query->result_array(); 
print_r($result); 
1

我想你想的交叉连接。 这里是你的codeigniter代码

$this->db->from("images ,note"); 
$this->db->select("images.*,note.*); 
//make sure both dont have same column name other wise use this 
//$this->db->select("images.column1,images.column2,note.column1); 

$this->db->orderby("images.date", "DESC"); 
//you can add more orderby 
//you can add where condition too 

$result=$this->db->get()->result_array(); 

现在你会得到交叉产品。 希望它能帮助你。