Question

I need some help with a SQL query. An additional requirement is that it must work on mysql, which does not have support for FULL OUTER JOINs (if that is indeed part of the answer).

I'm not terrible at SQL, but this is pretty tricky. I've tried a few things, but I'm not sure where to start, to be honest.

In essence, I want to union values from two columns in two different tables: But the two tables have different mumber of rows, where Table A has more rows than Table B. For the missing columns in Table B, I want to pad out with the values from the row in Table A which I am linking to.

My real life example, is that Table A contains a list of Concepts, each with a term, and Table B provides synonyms for the Concept's term in Table A.

I need to union the terms and synonyms (but not concat) into one column, and then calculate the length of the unioned term in a separate column. In the end, the record will be used to create a Solr Document with a single, unioned, indexed, and searchable column. I will then Sort my queries by the shortest term or synonym (hence the length column).

But let's simplify this a bit, and try to come up with a tightly defined problem statement. Walk before running :-)

I have two tables:

Table A
+----+-------+-------+-------+
| ID | Col_a | Col_b | Col_c |
+----+-------+-------+-------+
| 1  |   a0  |   b0  |   c0  |
| 2  |   a1  |   b1  |   c1  |
| 3  |   a2  |   b2  |   c2  |
+----+-------+-------+-------+


Table B
+----+------+-------+
| ID | A_ID | col_d |
+----+------+-------+
| 10 |   1  |  d0   |
| 11 |   1  |  d1   |
| 12 |   2  |  d2   |
| 13 |   2  |  d3   |
| 14 |   2  |  d4   |
| 15 |   3  |  d5   |
| 16 |   3  |  d6   |
+----+------+-------+


From these two tables, I need to generate these records (a union of col_c and col_d, padding with all rows from Table A):

Table Result:

+------+-------+-------+-----------+
| A_ID | col_a | col_b | col_union |
+------+-------+-------+-----------+
|    1 | a0    | b0    | c0        |
|    1 | a0    | b0    | d0        |
|    1 | a0    | b0    | d1        |
|    2 | a1    | b1    | c1        |
|    2 | a1    | b1    | d2        |
|    2 | a1    | b1    | d3        |
|    2 | a1    | b1    | d4        |
|    3 | a2    | b2    | c2        |
|    3 | a2    | b2    | d5        |
|    3 | a2    | b2    | d6        |
+------+-------+-------+-----------+


I also need to find the length of col_union and return this in another column, as well:

Table Result (with length):

+------+-------+-------+-----------+----------------+
| A_ID | col_a | col_b | col_union | len(col_union) |
+------+-------+-------+-----------+----------------+
|    1 | a0    | b0    | c0        | len_c0         |
|    1 | a0    | b0    | d0        | len_d0         |
|    1 | a0    | b0    | d1        | len_d1         |
|    2 | a1    | b1    | c1        | len_c1         |
|    2 | a1    | b1    | d2        | len_d2         |
|    2 | a1    | b1    | d3        | len_d3         |
|    2 | a1    | b1    | d4        | len_d4         |
|    3 | a2    | b2    | c2        | len_c2         |
|    3 | a2    | b2    | d5        | len_d5         |
|    3 | a2    | b2    | d6        | len_d6         |
+------+-------+-------+-----------+----------------+

and I have a feeling I'm going to struggle for the syntax for this. But the hard problem, for me, is just doing this JOIN (I think).

Hope someone can help! This is a bit beyond me....

Many thanks!

Henrik

Was it helpful?

Solution

Try this,

Select ID,col_a,col_b,col_union, 'Len_' + col_union,Length(col_union) as Ln from 
(
Select ID,col_a,col_b,Col_c as col_union from myUnionA 
Union All
Select A_ID,a.col_a,a.col_b,Col_d as col_union from  myUnionb  as b
inner join myUnionA as a on  a.ID = b.A_ID
)a
order by ID,col_union

DEMO LINK

Please clarify, LOGIC of your len columns, I think don't think what I have given is your requirement for len column.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top