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: -
table members (
-
member_id int(10),
-
...
-
recruiter_id int(10) /* id of member who recruited this member (0 if none) */
-
)
-
-
table answers (
-
...
-
member_fk int(10) /* id of member who posted this answer */
-
)
-
-
table questions (
-
...
-
member_fk int(10) /* id of member who posted this question */
-
)
-
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.
6 1786
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.
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. -
CREATE TABLE `members` (
-
`member_id` int(10) NOT NULL auto_increment,
-
`first_name` varchar(50) NOT NULL,
-
`last_name` varchar(50) NOT NULL,
-
`username` varchar(20) NOT NULL,
-
`password` varbinary(32) NOT NULL,
-
`created` datetime NOT NULL default '0000-00-00 00:00:00',
-
`last_login` datetime NOT NULL default '0000-00-00 00:00:00',
-
`recruiter` int(10) NOT NULL default '0',
-
PRIMARY KEY (`member_id`),
-
UNIQUE KEY `username` (`username`)
-
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-
-
CREATE TABLE `answers` (
-
`answer_id` int(10) NOT NULL auto_increment,
-
`member_fk` int(10) default NULL,
-
`question_fk` int(10) default NULL,
-
`created` datetime default NULL,
-
PRIMARY KEY (`answer_id`)
-
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-
-
CREATE TABLE `questions` (
-
`question_id` int(10) NOT NULL auto_increment,
-
`text` varchar(255) NOT NULL,
-
`member_fk` int(10) NOT NULL default '0',
-
`created` datetime NOT NULL default '0000-00-00 00:00:00',
-
`views` int(10) NOT NULL default '0',
-
`category_fk` int(10) NOT NULL default '0',
-
`enabled` tinyint(1) NOT NULL default '1',
-
PRIMARY KEY (`question_id`)
-
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-
Anybody have an idea?
So are the total points given by - select
-
(3 * (select count(a.answer_id) from answers a where a.member_fk = m.member_ID)
-
-
+ (10 * (select count(q.question_id) from questions q where a.member_fk = m.member_ID)
-
+ (15 * (select count(m1.recruiter) from members m1 where m1.recruiter = m.member_ID)
-
from members m
?
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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'...
|
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(*)
|
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) |
|
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.
| |
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...
|
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
|
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
|
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',...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |