vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   [MySQL] Help Optimize queries to select data from 2 tables (https://vborg.vbsupport.ru/showthread.php?t=78982)

mtha 03-28-2005 10:13 PM

[MySQL] Help Optimize queries to select data from 2 tables
 
I want to do the following two thing:


1- Select all posts (with its information) belong to one thread
something like:

PHP Code:

$threadid == X;

$posts $DB_site->query("
                    SELECT post.*, post.userid FROM post WHERE (post.threadid = 
$threadid)
"
);

$post $DB_site->fetch_array($posts

giving me a list of posts, says 15 posts (15 rows)


2. For each posts (each row), there is one userid, I need to select all awards that belong to that userid
something like

PHP Code:

$alluserawards =  $DB_site->query("
                    SELECT award.* FROM award WHERE userid=
$post[userid]
"
); 

giving me a list of awards, says 5 awards (5 rows)

if I run query #2 for each of the rows, it will run 15 times, + 1 query for #1,

is there any way to optimize the queries? so that I can somehow get a list of awards for each userid.

Dean C 03-28-2005 11:18 PM

Untested:

[sql]
SELECT post.*
awards.*
FROM " . TABLE_PREFIX . "post as post
LEFT JOIN
post
ON
post.userid = award.userid
[/sql]

You didn't give any info on your db schema for the awards table so I'm afraid I can't be of much help other than the query above which may or may not work depending what you have in the awards table :)

mtha 03-29-2005 12:57 AM

Quote:

Originally Posted by Dean C
Untested:

[sql]
SELECT post.*
awards.*
FROM " . TABLE_PREFIX . "post as post
LEFT JOIN
post
ON
post.userid = award.userid
[/sql]

You didn't give any info on your db schema for the awards table so I'm afraid I can't be of much help other than the query above which may or may not work depending what you have in the awards table :)

yeah, my mind was just out :D the query is kind of simple. Thanks Dean C
The db schema is available in my new hack
https://vborg.vbsupport.ru/showthread.php?t=78934

PHP Code:

            $userawards =  $DB_site->query("
                SELECT a.*, au.*, post.userid, post.postid 
             FROM " 
TABLE_PREFIX "post, " TABLE_PREFIX "award_user au, " TABLE_PREFIX "award a 
             WHERE 
$postids AND au.userid=post.userid AND a.award_id=au.award_id
                GROUP BY au.issue_id
            "
); 


Marco van Herwaarden 03-29-2005 03:17 AM

You really should use the LEFT JOIN like Dean showed you.

Also you will need to add an AS to the FROM clauses:
[sql]" . TABLE_PREFIX . "post AS post[/sql]
Or things will go wrong if a table prefix is used.

Xenon 03-29-2005 10:58 AM

Dean's left join won't work, as it would just select ONE award per post.

you have two possibilities:
1) put both tables into the from tag
2) cache the results of query two with one big query, and then work with the cache

mtha 03-29-2005 03:01 PM

Quote:

Originally Posted by MarcoH64
You really should use the LEFT JOIN like Dean showed you.

Also you will need to add an AS to the FROM clauses:
[sql]" . TABLE_PREFIX . "post AS post[/sql]
Or things will go wrong if a table prefix is used.

Got it work perfectly here
https://vborg.vbsupport.ru/showthread.php?t=78934

Thank you very much :)

mtha 04-02-2005 07:57 PM

Quote:

Originally Posted by Dean C
Untested:

[sql]
SELECT post.*
awards.*
FROM " . TABLE_PREFIX . "post as post
LEFT JOIN
post
ON
post.userid = award.userid
[/sql]

Could you tell me the different between ON and USING on JOIN command?

says

LEFT JOIN post ON (userid)
and
LEFT JOIN post USING (userid)

Xenon 04-03-2005 03:26 PM

using just allows a column-name, ON instead allows more complex conditions like for example:

[sql]SELECT * FROM post LEFT JOIN user ON (user.userid = post.userid * 2)[/sql]
or whatever (not that the statement above would be usefull ;))


All times are GMT. The time now is 12:03 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.01122 seconds
  • Memory Usage 1,740KB
  • 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
  • (3)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (8)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