Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 04-16-2008, 04:04 AM
noonespecial noonespecial is offline
 
Join Date: Nov 2002
Posts: 250
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Driving Me Crazy (MYQL Help Please)

I have a table (links_favorites) with two columns (linkid and userid).

I want to write something that counts the number of matches (in linkid) between two different userids.

So it something that grabs all the linkids from userid A - and then all the linkids from userid B - and then counts all the places where this intersects.

However, I simply can't figure out the best/easiest way to do this.

Could someone help please?
Reply With Quote
  #2  
Old 04-16-2008, 04:41 AM
nighthalk nighthalk is offline
 
Join Date: Oct 2005
Location: Maryland
Posts: 123
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't have your database to test this. So here you can test it.

Code:
<?php
   $sql="SELECT linkid, userid, FROM links_favorites WHERE MATCH (linkid,userid)";
   $result = mysql_query($sql);
   $resultsnumber = mysql_numrows($result);

echo "$resultsnumber";
?>
Let me know if this works.
Reply With Quote
  #3  
Old 04-16-2008, 07:35 AM
noonespecial noonespecial is offline
 
Join Date: Nov 2002
Posts: 250
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Problem with this is that I need to be able to specify the userids ...

IE: there are 4 matches in LINKID between userid-A and userid-B

Where would I specify those in this example?

--------------- Added [DATE]1208335849[/DATE] at [TIME]1208335849[/TIME] ---------------

Code:
   $sql="SELECT linkid, userid FROM links_favorites WHERE userid=1083";
   $result = mysql_query($sql);  
   $number1 = mysql_fetch_array($result);
   
   $sql2="SELECT linkid, userid FROM links_favorites WHERE userid=1";
   $result2 = mysql_query($sql2);  
   $number2 = mysql_fetch_array($result2);
   
   $result3 = array_intersect($number1, $number2);
   
   $joined = count($result3);
   
   echo $joined;
Also doesn't work.

--------------- Added [DATE]1208335899[/DATE] at [TIME]1208335899[/TIME] ---------------

Code:
     $sql="SELECT linkid FROM links_favorites WHERE userid=1083";
   $result = mysql_query($sql);  
   $number1 = mysql_fetch_array($result);
   
   $myids=implode(",", $number1);
   
   $sql2="SELECT COUNT(linkid) as count FROM links_favorites WHERE userid=1 AND linkid IN (".$myids.")";
   $result2 = mysql_query($sql2);  
   $number2 = mysql_fetch_array($result2);
Also doesn't work.

--------------- Added [DATE]1208337252[/DATE] at [TIME]1208337252[/TIME] ---------------

Code:
$fandom = $vbulletin->db->query_read("SELECT *
FROM links_favorites AS links_favorites
LEFT JOIN links_favorites AS favs2 ON (links_favorites.linkid=favs2.linkid)
WHERE links_favorites.userid=".$vbulletin->userinfo['userid']."
AND favs2.userid=".$friend[userid]."
");

$joined= $db->num_rows($fandom);
I think this works ..... ugly.
Reply With Quote
  #4  
Old 04-16-2008, 07:37 PM
MoT3rror MoT3rror is offline
 
Join Date: Mar 2007
Posts: 423
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$linkcount $db->query_first("
     SELECT COUNT(linkid) AS linkcount 
     FROM links_favorites 
     WHERE userid IN (1, 2)
"
); 
Reply With Quote
  #5  
Old 04-16-2008, 07:53 PM
Farcaster Farcaster is offline
 
Join Date: Dec 2005
Posts: 386
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

If I am understanding your question correctly and you want to know how many links User 1 and User 2 share, then you could use this query:

[SQL]SELECT
COUNT(DISTINCT t1.linkid) AS shared_links
FROM
link_favorites t1
INNER JOIN link_favorites t2 ON
t1.linkid = t2.linkid
WHERE
t1.userid = 1
AND t2.userid = 2[/SQL]

Your inclination to use a self-join in your example is pretty close to the mark, except that you could get duplicates and you might as well let MySQL handle the counting. Also, a left join would return all of user 1's links, whether they matched or not.
Reply With Quote
  #6  
Old 04-17-2008, 05:38 AM
Eikinskjaldi's Avatar
Eikinskjaldi Eikinskjaldi is offline
 
Join Date: Feb 2006
Location: Hell, never looked better
Posts: 572
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

or you could use a nested query:

[sql]
select count(distinct linkid) from link_favourites
where userid=1
and linkid in
(select distinct linkid from link_favourites where userid=2)
[/sql]
Reply With Quote
  #7  
Old 04-17-2008, 05:41 AM
noonespecial noonespecial is offline
 
Join Date: Nov 2002
Posts: 250
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:10 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03801 seconds
  • Memory Usage 2,218KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (4)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete