0

So, I have two tables,

In the first one I have some data that is co-related to the second table.

The first table This is the structure of the first table.

Now the second table is as follows.

enter image description here

The first table has a list of questions which have multiple values in the second table.

I wrote this query but it is not giving me the results which I want. Also I guess my query is not optimum.

SELECT a.programming_question_bank_id AS id, a.programming_question_bank_questions_text AS question, a.programming_questions_bank_skill_set AS skill_set, a.programming_questions_bank_topic_id AS topic_id, b.programming_test_i_o_input AS stdInput, b.programming_test_i_o_output AS stdOutput FROM (
SELECT programming_question_bank_id, programming_question_bank_questions_text, programming_questions_bank_skill_set, programming_questions_bank_topic_id
FROM client_trials.programming_test_question_bank WHERE programming_question_bank_id = 1)a
INNER JOIN 
(SELECT programming_test_i_o_question_id, programming_test_i_o_input, programming_test_i_o_output
FROM client_trials.programming_test_i_o WHERE programming_test_i_o_question_id = 1
)b ON a.programming_question_bank_id = b.programming_test_i_o_question_id

The output Which I am getting is as follows:

enter image description here

But I want the output something like this:

+----+---------------+-----------+----------+----------------+----------------+----------------+-----------------+-----------------+-----------------+
| id | question      | skill_set | topic_id | stdInput 1     | stdInput 2     | stdInput 3     | stdOutput 1     | stdInput 2      | stdInput 3      |
+----+---------------+-----------+----------+----------------+----------------+----------------+-----------------+-----------------+-----------------+
| 1  | Test Question | java-1    | 1        | test 1 input 1 | test 1 input 2 | test 1 input 3 | test 1 output 1 | test 1 output 2 | test 1 output 3 |
+----+---------------+-----------+----------+----------------+----------------+----------------+-----------------+-----------------+-----------------+

Also, I wanted to write a Stored procedure, but I can't understand where to begin.

Any help is appreciated.

SQL for creating tables:

CREATE TABLE `programming_test_question_bank` (
  `programming_question_bank_id` int(11) NOT NULL AUTO_INCREMENT,
  `creator_id` varchar(255) NOT NULL,
  `programming_question_bank_questions_text` mediumtext NOT NULL,
  `programming_questions_bank_standard_input` varchar(255) NOT NULL,
  `programming_questions_bank_skill_set` varchar(255) NOT NULL,
  `programming_questions_bank_topic_id` int(11) NOT NULL,
  `programming_questions_bank_standard_output` varchar(255) NOT NULL,
  `programming_questions_bank_created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `programming_questions_bank_updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`programming_question_bank_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='All Programming questions should be added here'
CREATE TABLE `programming_test_i_o` (
  `programming_test_i_o_id` int(11) NOT NULL AUTO_INCREMENT,
  `programming_test_i_o_question_id` int(11) NOT NULL,
  `programming_test_i_o_input` varchar(255) NOT NULL,
  `programming_test_i_o_output` varchar(255) NOT NULL,
  `programming_test_i_o_created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `programming_test_i_o_updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`programming_test_i_o_id`),
  KEY `programming_test_i_o_FK` (`programming_test_i_o_question_id`),
  CONSTRAINT `programming_test_i_o_FK` FOREIGN KEY (`programming_test_i_o_question_id`) REFERENCES `programming_test_question_bank` (`programming_question_bank_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4

INSERT STATEMENTS

INSERT INTO client_trials.programming_test_question_bank
(creator_id, programming_question_bank_questions_text, programming_questions_bank_standard_input, programming_questions_bank_skill_set, programming_questions_bank_topic_id, programming_questions_bank_standard_output, programming_questions_bank_created_at, programming_questions_bank_updated_at)
VALUES('1', 'Test Question - 1', '1,2', 'java-1', 1, '1,2', current_timestamp(), current_timestamp());


INSERT INTO client_trials.programming_test_i_o
(programming_test_i_o_question_id, programming_test_i_o_input, programming_test_i_o_output, programming_test_i_o_created_at, programming_test_i_o_updated_at)
VALUES(2, 'test 2 input 3', 'test 2 output 3', current_timestamp(), current_timestamp());
johnny68
  • 421
  • 1
  • 6
  • 21
  • No images please. Please frame SQL questions with **relevant and minimal sample data** showcasing your requirements, and **corresponding expected output**. Please read this link: [Why should I provide a Minimal Reproducible Example for a very simple SQL query?](https://meta.stackoverflow.com/q/333952/2469308) – Madhur Bhaiya Oct 02 '19 at 08:57
  • Questions like these are already answerd -> re/search [MySQL pivot](https://stackoverflow.com/search?q=MySQL+pivot) or [MySQL convert records into columns](https://stackoverflow.com/search?q=MySQL%20convert%20records%20into%20columns) on this website – Raymond Nijland Oct 02 '19 at 09:21

0 Answers0