Jump to content

Glued 2 tablets side by side...


Matpatnik
 Share

Recommended Posts

Hi guys, I came with a problem and can't find the solution. I looked on internet everywhere I could and here is the closest I found but still not right.

The problem here is it cut the last part of the my longest table. The 2 tables doesn't have the same rows number but I need them all and I only need 1 category to be shown. 

So I need the 2 tables be glued together side by side filtered by category ( both table share the same categories ) with no missing data (of that category)

So far this is what I got. I tried to join but it keep duplicating rows so instead of getting 200 rows I get like 3k rows. I tried union too and no success. I'm out of inspiration right now.

If you can point a right or better path it would be great. 

Here's my query so far:

SELECT t1.`id`, t1.c1, t1.`total`, t2.`id`, t2.c2, t2.`total`

FROM (

SELECT @i:=@i+1 AS rowId, `id`, `categories` as c1, `total` FROM extable17, (SELECT @i:=0) a ) AS t1,

(SELECT @j:=@j+1 AS rowId, `id`, `categories` AS c2, `total` FROM extable18, (SELECT @j:=0) a ) AS t2

WHERE t1.rowId = t2.rowId;

Link to comment
Share on other sites

This was a great puzzle to work out!

But would something like this work out for you?

SELECT t1.`id`, t1.c1, t1.`total`, if(t2.rowId = t1.rowId, t2.`id`, null) as id, if(t2.rowId = t1.rowId, t2.c2, null) as c2, if(t2.rowId = t1.rowId, t2.`total`, null) as total

FROM 

(SELECT @i:=@i+1 AS rowId, `id` as id, `categories` as c1, `total` as total FROM extable17, (SELECT @i:=-1) a ) AS t1,

(SELECT @j:=@j+1 AS rowId, `id` as id, `categories` AS c2, `total` as total FROM extable18, (SELECT @j:=-1) a ORDER BY c2 ASC ) AS t2

WHERE t1.rowId % @j = t2.rowId

Given that there are more records in t1 than t2 this will hold.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...