0

I have two array taken out from table basic on the chapter id. Where one array is in condition where chapter is between 1 and 6. Similarly another array is in condition where chapter is between 7 to 12 from the same table.

where my first query is

$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';

and second query is

$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';

All are same what differs is title according to chapter.

When I do array merge than only one title is shown but I want to make double array to single and showing both title. HOw can i do it?

Thanks in advance...

1 Answer 1

1

You can do this directly in MySQL, by UNION(implicit distinct) or UNION ALL the two queries, something like:

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;

Then execute this query only and you will have both the results sets into one array coming from this query.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.