Jump to content

Muliti Table Join


dyluck

Recommended Posts

Hi there,

 

I am creating a query that involves 3 tables total.

How would you do this query? 

 

Table 1

TID

MNAME

1

JOHN

2

JIM

 

Table 2

MNAME

FULLNAME

JOHN

JOHN HENRY

JIM

JIM JONES

 

Table 3

TID

VIEWCOUNT

1

1

1

1

2

1

 

 

I am doing 1 query that will hopfully look something like this:

SELECT * FROM TABLE 1  JOIN THE REST TO LOOK LIKE THIS:

 

JOHN HENRY    2

JIM JONES      1

 

Thanks for all your help!  The beast of a query I attempted crashed mysql server :P

Link to comment
Share on other sites

It depends exactly what you want. If you want results regardless of whether they are in all the tables, that will be a join. Otherwise, you can do a simple query like this:

 

SELECT * FROM table1 a, table2 b, table3 c WHERE a.mname = b.mname AND a.tid = c.tid
**OR**
SELECT a.*, b.fullname, c.viewcount FROM table1 a, table2 b, table3 c WHERE a.mname = b.mname AND a.tid = c.tid

Link to comment
Share on other sites

F1Fan: those SQLs don't produce the desired output.

 

Not tested, but it should work.

SELECT t2.fullname, SUM(t3.viewcount) cnt

FROM table1 t1

INNER JOIN table2 t2

    ON (t1.mname = t2.mname)

INNER JOIN table3 t3

    ON (t1.tid = t3.tid)

GROUP BY t3.tid;

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.