473,770 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question About Inner Joins and COUNT()

4 New Member
I'm trying to write a query that selects the top five members of a forum based on how many total points they have. A member's total points are calculated as follows: (3 * # answers posted) + (10 * # questions posted) + (15 * # members recruited). The relevant parts of the tables are listed below:

Expand|Select|Wrap|Line Numbers
  1. table members (
  2.      member_id int(10),
  3.      ...
  4.      recruiter_id int(10)  /* id of member who recruited this member (0 if none) */
  5. )
  6.  
  7. table answers (
  8.      ...
  9.      member_fk int(10)  /* id of member who posted this answer */
  10. )
  11.  
  12. table questions (
  13.      ...
  14.      member_fk int(10)  /* id of member who posted this question */
  15. )
  16.  
Can this be done in a single query or do I have to total the answer, question, and recruitment points separately? I tinkered around with some inner joins and count()s and got nowhere.
Jul 21 '08 #1
6 1786
r035198x
13,262 MVP
It should be possible with one query. Post the query you've tried and the relevant parts of the tables. The table structures you posted above are missing the important columns.
Jul 22 '08 #2
loffing4
4 New Member
The full table structures are below. Any query I wrote isn't worth posting as I'm not really sure how to get started. I would think I'd need to inner join the tables based on the member id, but I couldn't get it to work. I'm pretty new to mySQL and databases in general.

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `members` (
  2.   `member_id` int(10) NOT NULL auto_increment,
  3.   `first_name` varchar(50) NOT NULL,
  4.   `last_name` varchar(50) NOT NULL,
  5.   `username` varchar(20) NOT NULL,
  6.   `password` varbinary(32) NOT NULL,
  7.   `created` datetime NOT NULL default '0000-00-00 00:00:00',
  8.   `last_login` datetime NOT NULL default '0000-00-00 00:00:00',
  9.   `recruiter` int(10) NOT NULL default '0',
  10.   PRIMARY KEY  (`member_id`),
  11.   UNIQUE KEY `username` (`username`)
  12. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
  13.  
  14. CREATE TABLE `answers` (
  15.   `answer_id` int(10) NOT NULL auto_increment,
  16.   `member_fk` int(10) default NULL,
  17.   `question_fk` int(10) default NULL,
  18.   `created` datetime default NULL,
  19.   PRIMARY KEY  (`answer_id`)
  20. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
  21.  
  22. CREATE TABLE `questions` (
  23.   `question_id` int(10) NOT NULL auto_increment,
  24.   `text` varchar(255) NOT NULL,
  25.   `member_fk` int(10) NOT NULL default '0',
  26.   `created` datetime NOT NULL default '0000-00-00 00:00:00',
  27.   `views` int(10) NOT NULL default '0',
  28.   `category_fk` int(10) NOT NULL default '0',
  29.   `enabled` tinyint(1) NOT NULL default '1',
  30.   PRIMARY KEY  (`question_id`)
  31. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
  32.  
Jul 25 '08 #3
loffing4
4 New Member
Anybody have an idea?
Jul 30 '08 #4
r035198x
13,262 MVP
Anybody have an idea?
So are the total points given by

Expand|Select|Wrap|Line Numbers
  1. select 
  2. (3 * (select count(a.answer_id) from answers a where a.member_fk = m.member_ID) 
  3.  
  4. + (10 * (select count(q.question_id) from questions q where a.member_fk = m.member_ID) 
  5. + (15 * (select count(m1.recruiter) from members m1 where m1.recruiter = m.member_ID) 
  6. from members m
?
Jul 31 '08 #5
loffing4
4 New Member
Perfect! I just had to add a column alias and an order by clause.

The nested select method wasn't one I was familiar with.
Jul 31 '08 #6
r035198x
13,262 MVP
Perfect! I just had to add a column alias and an order by clause.

The nested select method wasn't one I was familiar with.
Glad you managed to finish it off yourself. Good luck with the rest of it.
Aug 1 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6416
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName, Employees.LastName, TerritoryID, Employees.EmployeeID, RegionID, ProductID from Employees
8
6325
by: kieran | last post by:
Hi, I have the following sql statement. I originally had the statement with two INNER JOINS but in some situations was getting an error so changed the last INNER JOIN to a LEFT OUTER JOIN (as is seem below). This seemed to work but i am unsure why and would like to know in case it falls over again. Why did the two INNER JOINS not work, and am I correct to use the LEFT OUTER JOIN in this context. As you can see the table 'tblStaff1_2'...
5
3391
by: KGuy | last post by:
I have a question on a practice assignment that I can't solve. Can someone help me out? Question: The table Arc(x,y) currently has the following tuples (note there are duplicates): (1,2), (1,2), (2,3), (3,4), (3,4), (4,1), (4,1), (4,1), (4,2). Compute the result of the query: SELECT a1.x, a2.y, COUNT(*)
11
6975
by: Dave [Hawk-Systems] | last post by:
have the table "numbercheck" Attribute | Type | Modifier -----------+------------+---------- svcnumber | integer | not null svcqual | varchar(9) | svcequip | char(1) | svctroub | varchar(6) | svcrate | varchar(4) | svcclass | char(1) | trailer | varchar(3) |
26
2177
by: Jeff | last post by:
Ok gang. Here is something complicated, well, at least to me anyway. Using Access DB I have a table in my DB called members. In that table, I have 2 tables I will be using "username" and "points" Now, I also have a table called all_matches. This table contains every match report. Over 25,000 of them. I have a "username" field an "outcome" field an "username1" field and "extra_match" field.
2
2397
by: Fendi Baba | last post by:
I created a person table with various fields such as Suffix, Salutation, etc, Some of these fields may not be mandatory for example suffix. In the actual table itself, I only have a field for suffix ID where 1=Phd, 2= MD. To display all of these to the user, I created a form with an underlying query. The problem I am encountering is this, when we have an empty field, for example where ID="", the query returns nothing. How do i work around...
6
9315
by: dmonroe | last post by:
hi group -- Im having a nested inner join problem with an Access SQl statement/Query design. Im running the query from ASP and not usng the access interface at all. Here's the tables: tblEmployees empId -- EmpName -- EmpRole -- EmpManager -------....------------.... ---------....--------------- 1........ dan yella..........1..........2
52
6350
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
0
1285
by: stanlew | last post by:
Happy New Year everyone! I'm new to both T-SQL and this forum. I'm currently doing an internship and my first task was to create a small program which will send an email detailing the sales of the previous day versus monthly targets and sales. Most of the parts were figured out and eveything was done in Visual Studio. The gist of the code was written in one large chunk of SQL code, as below: SELECT derivedtbl_1.family AS 'Family',...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10230
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10004
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9870
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8886
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.