简体   繁体   中英

How to write this basic sql statement

So I have 4 tables that are connected via foreign keys namely result, position, student, candidates

What i need to achieve is this: output:

------------------------
 s_fname | count(c_id)
-----------------------
   Mark  |     2       -> President
   France|     2       -> President

.. to count as to how many times a c_id have been repeated in the table "result" which is also filtered by pos_id from the "candidates" table

Below is my code which lacks the counting part:

select s_fname 
from results, candidates, student, positioning 
where results.c_id = candidates.c_id 
  AND student.sid = results.sid 
  AND candidates.pos_id = positioning.pos_id 
  AND positioning.pos_id = 1 
Group BY results.sid;

..which I know lacks a lot of things ...

Thanks

it seems very complex to me but i know there are gurus here who can achieve this,

result table

---------------------
| r_id | sid | c_id |
---------------------
    1   |  1  |   1
    2   |  1  |   2
    3   |  1  |   4
    4   |  2  |   1
    5   |  2  |   2
    6   |  2  |   4
    7   |  3  |   3
    8   |  3  |   2
    9   |  5  |   3
    10  |  5  |   2

----------------------
student table
----------------
| s_id| s_fname|
----------------
    1  |  Mark
    2  |  Jorge
    3  |  France
    4  |  James

--------------------
Candidates Table
------------------------
| c_id | sid  |  pos_id
------------------------
    1   |   1  |     1
    2   |   2  |     2
    3   |   4  |     3
    4   |   3  |     1
    5   |   5  |     2


----------------------
positioning Table
-----------------------
| pos_id  |  po_name |
-----------------------
 1    |  President
 2    |  Vice President
 3    |  Secretary
 4    |  Treasurer

This is untested, but should return your intended result.

What it does is joins all of your tables on the related foreign keys , effectively giving a wide table of all of your columns. Then we limit on the candidates that are running for the President position. Since we need to group because of the count aggregate we group on the name . The count should reflect the number of votes they got, because there is a one to many relationship to the result table.

SELECT s_fname, Count(*) 
FROM studentTable st 
INNER JOIN Candidates c On c.sid = st.s_ID 
INNER JOIN positioning p on c.pos_ID = p.pos_ID
INNER JOIN results r on st.s_ID = r.s_ID
WHERE po_Name = "President"
GROUP BY s_Fname 

Due to a misunderstanding of the intended joins, the following query should show the appropriate results.

SELECT s_fname, Count(*) 
FROM studentTable st 
INNER JOIN Candidates c On c.sid = st.s_ID 
INNER JOIN positioning p on c.pos_ID = p.pos_ID
INNER JOIN results r on c.c_ID = r.c_ID
WHERE po_Name = "President"
GROUP BY s_Fname 

Code:

SELECT s_fname AS [Student Name], COUNT(A.c_id) AS [Count], po_name AS [Position]
FROM results AS A INNER JOIN candidates AS B ON A.c_id=B.c_id
                  INNER JOIN student AS C ON A.sid=C.sid
                  INNER JOIN positioning AS D ON B.pos_id=D.pos_id
WHERE B.pos_id = 1 
GROUP BY s_fname
SELECT s.s_fname, COUNT(*), p.po_name
    FROM students s 
    JOIN candidates c ON c.s_id = s.s_id
    JOIN positioning p ON c.pos_id = p.pos_id
    JOIN results r ON s.s_id = r.s_id
WHERE p.pos_id = 1
GROUP BY s.s_id

http://sqlfiddle.com/#!2/9472a/17

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM