Sundar Ban
Sundar Ban

Reputation: 619

how to get both array in single array without losing the values in mysql and php?

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...

Upvotes: 0

Views: 52

Answers (1)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79959

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.

Upvotes: 1

Related Questions