Jump to content

Join 4 tables


rashmi_k28

Recommended Posts

Hi,

 

Here is the queries below.

How to join all the tables into one single query

 

select cid from c_table limit 1;

select * from g_table where cid='4';

select * from a_table where gid='43';

select count(*) from k_table where aid='48';

 

I have to remove limit from c_table,

I have to fetch all the cid's from c_table where count(*) from k_table is greater than 1

Link to comment
Share on other sites

Hi

 

Something like this:-

 

SELECT a.cid, count(kid) as KidCount
FROM c_table a
INNER JOIN g_table b ON a.cid = b.cid
INNER JOIN a_table c ON b.gid = c.gid
INNER JOIN k_table d ON c.aid = d.aid
GROUP BY a.cid
HAVING KidCount > 1

 

All the best

 

Keith

Link to comment
Share on other sites

Thanks for the query. This works.

How to get the count of a.cid where KidCount >=1

 

SELECT a.cid, count(kid) as KidCount
FROM c_table a
INNER JOIN g_table b ON a.cid = b.cid
INNER JOIN a_table c ON b.gid = c.gid
INNER JOIN k_table d ON c.aid = d.aid
GROUP BY a.cid
HAVING KidCount > 1

Link to comment
Share on other sites

Hi

 

SELECT a.cid, count(kid) as KidCount
FROM c_table a
INNER JOIN g_table b ON a.cid = b.cid
INNER JOIN a_table c ON b.gid = c.gid
INNER JOIN k_table d ON c.aid = d.aid
GROUP BY a.cid
HAVING KidCount >= 1

 

That should work.

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.