1

query

SELECT 
    (SELECT NAME FROM product_component) AS pcNAME,
    (SELECT PROJECT_NAME FROM jira_project) AS jpNAME, 
    (SELECT FILTER_NAME FROM jira_filter) AS jfNAME

Each SELECT will return an indeterminate number of rows. I get the error Subquery returns more than 1 row. My desired output will be something like this (quick sketch):

=======================================
|   pcNAME   |   jpNAME   |  jfNAME   |
=======================================
|    data    |    data    |    data   |
+------------+------------+-----------+
|    data    |    data    |    data   |
+------------+------------+-----------+
|    data    |    data    |    data   |
+------------+------------+-----------+
|            |    data    |    data   |
+------------+------------+-----------+
|            |    data    |    data   |
+------------+------------+-----------+
|            |    data    |           |
+------------+------------+-----------+

Each column may produce a different number of rows than the others. So I will want to produce the amount of rows from the max and then blank out the others that don't fill the max number of rows.

NOTE: None of these tables have a shared column so cannot achieve as INNER JOIN

Any ideas on how this can be achieved?

3
  • Do these table have auto increment id fields? If yes, you can use those to join them. Commented Nov 12, 2015 at 13:22
  • @GarethD the tuples do not matter. All I am attempting to do is display all data form those 3 tables. Data can be unordered Commented Nov 12, 2015 at 13:28
  • @Shadow yes, they all have a AI field Commented Nov 12, 2015 at 13:28

1 Answer 1

4

One way to handle this in MySQL to use to variables, union all and aggregation:

SELECT MAX(NAME) as NAME, MAX(PROJECT_NAME) as PROJECT_NAME,
       MAX(FILTER_NAME) as FILTER_NAME
FROM ((SELECT (@rnpc := @rnpc + 1) as rn, NAME, NULL as PROJECT_NAME, NULL as FILTER_NAME
      FROM product_component CROSS JOIN
           (SELECT @rnpc := 0) params
      ) UNION ALL
      (SELECT (@rnpn := @rnpn + 1) as rn, NULL, PROJECT_NAME, NULL as FILTER_NAME
      FROM jira_project CROSS JOIN
           (SELECT @rnpn := 0) params
      ) UNION ALL
      (SELECT (@rnf := @rnf + 1) as rn, NAME, NULL as PROJECT_NAME, NULL as FILTER_NAME
      FROM jira_filter CROSS JOIN
           (SELECT @rnf := 0) params
      )
     ) t
GROUP BY rn
ORDER BY rn;
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant answer!! Thank you. Never thought of the variables

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.