Mysql Query for Multiple table

MySQL

I have two table question and answer. The structure is given below

CREATE TABLE IF NOT EXISTS `question` (
`id` int(10) NOT NULL,
  `question` varchar(100) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

INSERT INTO `question` (`id`, `question`) VALUES
(1, 'Who is Mammotty'),
(2, 'Who Rahul Ghandi');

CREATE TABLE IF NOT EXISTS `answers` (
`id` int(10) NOT NULL,
  `q_id` int(10) NOT NULL,
  `answer` varchar(100) NOT NULL,
  `status` varchar(10) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

INSERT INTO `answers` (`id`, `q_id`, `answer`, `status`) VALUES
(1, 1, 'Actor', 'Y'),
(2, 1, 'Farmer', 'N'),
(3, 1, 'Engineer', 'N'),
(4, 1, 'Driver', 'N'),
(5, 2, 'Politician', 'Y'),
(6, 2, 'Teacher', 'N'),
(7, 2, 'Doctor', 'N'),
(8, 2, 'Pilot', 'N');

I want to display question and answer in this format

question         | ans1       | status1 | ans2   | status2 | ans3     | status3 | ans4   | status3
---------------------------------------------------------------------------------------------------
Who is Mammotty  | Actor      | Y       | Farmer | N       | Engineer | N       | Driver | N
Who Rahul Ghandi | Politician | Y       | Teacher| N       | Doctor   | N       | Pilot  | N

Can any one Help me ?

Best Answer

If each questions having exactly 4 answers the below query will help you. Same way you can include the status field.

SELECT qst.question, SUBSTRING_INDEX( group_concat( ans.answer ) , ',', 1 ) AS Ans1, SUBSTRING_INDEX( SUBSTRING_INDEX( group_concat( ans.answer ) , ',', 2 ) , ',', -1 ) AS Ans2, SUBSTRING_INDEX( SUBSTRING_INDEX( group_concat( ans.answer ) , ',', -2 ) , ',', 1 ) AS Ans3, SUBSTRING_INDEX( group_concat( ans.answer ) , ',', -1 ) AS Ans4 FROM question qst, answers ans WHERE ans.q_id = qst.id GROUP BY ans.q_id