0

我从章节 id 的基本表中取出了两个数组。其中一个数组处于章​​节在 1 到 6 之间的状态。类似地,另一个数组处于同一个表中章节介于 7 到 12 之间的状态。

我的第一个查询在哪里

$sql1 = 'SELECT s.section_number, s.title,n.description,s.global_order_id, c.commentary FROM section as s 
                INNER JOIN note as n 
                INNER JOIN commentary as c ON s.global_order_id=n.global_order_id and c.global_order_id=n.global_order_id 
                where n.user_id='.Yii::app()->user->id.' and s.chapter_id Between 1 and 6';

第二个查询是

$sql2 = 'SELECT s.section_number, s.title,n.description,s.global_order_id, c.commentary FROM section as s 
                INNER JOIN note as n 
                INNER JOIN commentary as c ON s.global_order_id=n.global_order_id and c.global_order_id=n.global_order_id 
                where n.user_id=' . Yii::app()->user->id . ' and s.chapter_id Between 7 and 12';

都是一样的,不同的是根据章节的标题。

当我进行数组合并时,只显示一个标题,但我想将双数组变为单个并显示两个标题。我该怎么做?

提前致谢...

4

1 回答 1

1

您可以通过UNION(隐式不同)或UNION ALL两个查询直接在 MySQL 中执行此操作,例如:

SELECT 
  s.section_number,  
  s.title,
  n.description,
  s.global_order_id, 
  c.commentary 
FROM section as s 
INNER JOIN note as n 
INNER JOIN commentary as c  ON s.global_order_id = n.global_order_id 
                           and c.global_order_id = n.global_order_id 
where n.user_id = '.Yii::app()->user->id.' 
  and s.chapter_id Between 1 and 6
UNION ALL
SELECT 
  s.section_number, 
  s.title,
  n.description,
  s.global_order_id, 
  c.commentary 
FROM section as s 
INNER JOIN note as n 
INNER JOIN commentary as c  ON s.global_order_id = n.global_order_id 
                           and c.global_order_id = n.global_order_id 
where n.user_id=' . Yii::app()->user->id . ' 
  and s.chapter_id Between 7 and 12;

然后仅执行此查询,您会将两个结果集放入来自此查询的一个数组中。

于 2013-02-17T06:55:56.863 回答