Jump to content
Matpatnik

Glued 2 tablets side by side...

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;

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...