vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   INNER JOIN query (https://vborg.vbsupport.ru/showthread.php?t=158641)

selwonk 09-24-2007 12:46 PM

INNER JOIN query
 
Hi folks

Apologies in advance if this has been covered - I couldn't find the answer having done a bit of searching...

I run vBadvanced and I want to produce a thank you box on the front page which shows the latest 10 positive reputation comments. The idea is to promote the reputation system and give people added incentive to give and receive rep points

The information I want is in the reputation and user table. I want to get the following fields:

reputation.postid
reputation.userid
reputation.whoadded
reputation.reason
user.username

I've got this working with the following query:
Code:

SELECT
        reputation.postid,
        reputation.userid,
        reputation.whoadded,
        reputation.reason,
        user.username
FROM
        reputation
INNER JOIN
        user
ON
        reputation.userid=user.userid
WHERE
        reputation.reputation >= 1 AND
        reputation.reason <> ''
ORDER BY
        reputation.dateline DESC
LIMIT 10;

However, I would also like to map reputation.whoadded to user.username as well. Is it possible to do two INNER JOINs to get this information?

I'd be very grateful of any advice. I'm not feeling well today, and my brain hurts :)

Thanks in advance...

Farcaster 09-24-2007 03:23 PM

Quote:

Originally Posted by selwonk (Post 1345973)
However, I would also like to map reputation.whoadded to user.username as well. Is it possible to do two INNER JOINs to get this information?

I'd be very grateful of any advice. I'm not feeling well today, and my brain hurts :)

Thanks in advance...

Yes, it is possible, you just need to use a table alias. So your join would look like this example:

[SQL]INNER JOIN user userwho ON userwho.userid = reputation.whoadded[/SQL]

And in your select, you'd reference it by userwho.username

Paul M 09-24-2007 05:38 PM

It's good practice to use 'AS' (INNER JOIN user AS userwho .....). :)

Farcaster 09-24-2007 05:46 PM

Quote:

Originally Posted by Paul M (Post 1346175)
It's good practice to use 'AS' (INNER JOIN user AS userwho .....). :)

It may make it slightly more readable if that is the format you are used to using, but I find it to be superfluous and I usually omit it.

selwonk 09-25-2007 07:42 AM

Ok folks - thanks for that. I'll give it a go and report back

Thank you

--------------- Added at 12:12 ---------------

Thanks chaps - that gave me what I needed. The resultant query looks like this:
Code:

SELECT
        reputation.postid,
        reputation.userid,
        reputation.whoadded,
        reputation.reason,
        user.username,
        userwho.username AS usernamefrom

FROM
        reputation

INNER JOIN
        (user INNER JOIN user AS userwho ON reputation.userid=user.userid) ON reputation.whoadded=userwho.userid

WHERE
        reputation.reputation >= 1 AND
        reputation.reason <> ''

ORDER BY
        reputation.dateline DESC
LIMIT 10;

Thanks again!


All times are GMT. The time now is 05:29 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01032 seconds
  • Memory Usage 1,725KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (2)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete