Im having a problem from mySQL , actually i want to show the fullname of the student with the highest votes in the subject.
here's the code
$query="SELECT student_tbl.fullname, student_tbl.subject,
COUNT(student_tbl.fullname)AS rating
FROM student_tbl
INNER JOIN subject_tbl
ON student_tbl.subject= subject_tbl.subjname
GROUP BY subject ORDER BY num,
COUNT(fullname) DESC";
student_tbl subject_tbl
| fullname | subject| | subjname | num |
==================== ================
| John | Math | | Math | 1 |
| Rey | Math | | Science | 2 |
| Wey | Science | | English | 3 |
| Xin | Science | =================
| Nick | English |
| Mi | English |
| John | Math |
| Xin | Science |
| Mi | English |
======================
Here's the output to my codes:
| fullname | Subject | Votes |
============================
| John | Math | 2 |
| Wey | Science | 2 |
| Nick | English | 2 |
And This is the output i want:
| fullname | Subject | Votes |
============================
| John | Math | 2 |
| Xin | Science | 2 |
| Mi | English | 2 |
SELECT s1.fullname, s1.subject, COUNT(s1.fullname) AS rating
FROM student_tbl s1
INNER JOIN subject_tbl s2
ON s1.subject= s2.subjname
GROUP BY subject, fullname
HAVING COUNT(fullname) = (
SELECT COUNT(fullname) FROM student_tbl
WHERE s1.subject = subject
GROUP BY fullname
ORDER BY COUNT(fullname) DESC
LIMIT 1
)
ORDER BY num, COUNT(fullname) DESC
SqlFiddle : http://sqlfiddle.com/#!2/aea20
I highly suggest you to use numeric keys and not string keys.
Not pretty but it works. I grouped the votes first and then got the rank.
SQL FIDDLE DEMO
SELECT fullname, subject, votes
FROM (SELECT fullname,subject,votes,
(CASE subject WHEN #curtype THEN #currow := #currow + 1
ELSE #currow := 1 AND #curtype := subject end ) + 1 AS rank
FROM (SELECT fullname,subject,Count(1) AS Votes
FROM student_tbl
GROUP BY fullname,subject) a,(SELECT #currow := 0, #curtype := '') r
ORDER BY subject,votes DESC) b
WHERE rank = 1
Related
Assalammualaikum..
I have database like this:
name | subjects | score
B | math | 70
B | math | 80
B | scien | 90
A | math | 80
And I have query like this:
$query = mysqli_query($db,"SELECT name, subject, score
(
CASE subject WHEN #curSubject
THEN #curRow := #curRow +1
ELSE #curRow :=1 AND #curSubject:= subject END
) AS rank
FROM table GROUP BY name,score ORDER BY name, score desc");
If I Select on SQL output from that query just like this:
name | subjects | score | rank
B | math | 80 | 1
B | math | 70 | 2
B | scien | 90 | 1
A | math | 80 | 1
My problem is, when I will echo using looping php code like this:
while ($array = mysqli_fetch_array($query)){
echo $array['rank']'';
}
output:
rank | rank (not like this)
1 | 1
1 | 2
1 | 1
1 | 1
How can the value rank be echoed? Thank you
Wassalammualaikum..
I'm needing to retrieve shared values from a table based on a value from another table, but don't show duplicates.
Example of what tables I have...
Table - members
+-----------------+
| ID | NAME |
+-----------------+
| 1 | Bob |
| 2 | Jack |
| 3 | Jane |
| 4 | Bruce |
| 5 | Clark |
| 6 | Peter |
+-----------------+
Table - groups
+--------------------------------+
| ID | NAME | MANAGER_ID |
+--------------------------------+
| 1 | Group A | 1 | (Bob)
| 2 | Group B | 2 | (Jack)
| 3 | Group C | 1 | (Bob)
+--------------------------------+
Table - group_members
+--------------------------------+
| ID | GROUP_ID | MEMBER_ID |
+--------------------------------+
| 1 | 1 | 3 | (Group A - Jane)
| 2 | 1 | 4 | (Group A - Bruce)
| 3 | 1 | 5 | (Group A - Clark)
| 4 | 1 | 6 | (Group A - Peter)
| 5 | 2 | 3 | (Group B - Jane)
| 6 | 3 | 4 | (Group B - Bruce)
| 7 | 3 | 5 | (Group C - Clark)
+--------------------------------+
What I am needing
(Note: I'm using * in queries here to shorten code.)
If 'Bob' sees all his groups.
Look at 'group_members' table and show all members that belong to it...
$q = SELECT * FROM groups WHERE manager_id = $id;
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch-assoc($r) {
$q2 = SELECT *, count(MEMBERS_ID) AS group_count FROM group_members LEFT JOIN members ON group_members.MEMBER_ID = members.id WHERE group_id = '$row[GROUP_ID]';
$r2 = mysqli_query($dbc, $q2);
while ($row2 = mysqli_fetch-assoc($r2) {
echo $row2['name'];
}
}
This shows me the list as expected.
+------------------------+
| NAME | GROUP COUNT |
+------------------------+
| Jane | 1 |
| Bruce | 1 |
| Clark | 1 |
| Peter | 1 |
| Bruce | 1 |
| Clark | 1 |
+------------------------+
I Add GROUP BY group_members.group_id to my 2nd query and that shows.
+------------------------+
| NAME | GROUP COUNT |
+------------------------+
| Jane | 1 |
| Bruce | 2 |
| Clark | 2 |
| Peter | 1 |
+------------------------+
Which is perfect... But here is the problem
if I add a WHERE members.name LIKE \'%clark%\' then it outputs...
+------------------------+
| NAME | GROUP COUNT |
+------------------------+
| | |
| | |
| Clark | 1 |
| | |
| | |
| Clark | 1 |
+------------------------+
It ignores GROUP BY and shows blank rows where other entries would show.
So with all that said. Does any one know why or a better way to do this please?
Been at it for a while now and would really appreciate any assistance.
EDITED:
Here's the full query with all the columns used:
$q = SELECT * FROM groups WHERE manager_id = $id;
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch-assoc($r) {
$q2 = SELECT members.id) AS memid, members.first, members.last, members.comname, members.email, members.sector, (members.status) AS memstatus, (group_members.id) AS groupid, (group_members.member_id) AS memidgroup, group_members.group_id, COUNT(group_members.member_id) AS groupcount, member_roles.role FROM members LEFT JOIN group_members ON members.id = group_members.member_id LEFT JOIN member_roles ON members.role_id = member_roles.id WHERE group_id = '$row[GROUP_ID]' AND members.name LIKE '%clark%' GROUP BY group_members.group_id;
$r2 = mysqli_query($dbc, $q2);
while ($row2 = mysqli_fetch-assoc($r2) {
echo $row2['name'];
}
}
Your query or problem is not completely stated. One cannot guess or assume.
Post your last query as well as all queries dont worry about saving the space.
Those blank rows have data that why they are in the table.
Base on your explanation or your query, here is my simple answer
SELECT id,
(select groups.id from groups where groups.id = group_members.group_id )AS group_members_id,
(select groups.name from groups where groups.id = group_members.group_id )AS group_members_name,
(select members.id from members where members.id = group_members.member_id )AS members_id,
(select members.name from members where members.id = group_members.member_id )AS members_name,
count((select members.id from members where members.id = group_members.member_id ) )as members_id_count FROM group_members WHERE (select members.name from members where members.id = group_members.member_id ) LIKE '%clark%' group by members_id
As you mentioned
WHERE members.name LIKE \'%clark%\'
you were selecting data from the members table whereas you have to select the data from group_members table.
select * from Teams;
| team_name | team_id |
| India | 1 |
| England | 2 |
| Germany | 3 |
| Japan | 4 |
select * from Matches;
| match_id | match_date | hometeam | awayteam | homescore | awayscore |
| 1 | 2014-06-24 | 1 | 2 | 3 | 0 |
| 2 | 2014-06-25 | 3 | 4 | 0 | 2 |
Matches-table hometeam and awayteam are foreign keys for Teams-table team_id
I want a select that displays the homescore and awayscore along with team_names
You have to join matches table with teams table like this:
SELECT th.team_name as team_home, m.homescore, ta.team_name as team_away, m.awayscore
FROM matches m
INNER JOIN teams th ON m.hometeam = th.team_id
INNER JOIN teams ta ON m.awayteam = ta.team_id
Here is SqlFiddle
Another one, with entire match data in one select...
select
m.*,
home.team_name as hometeam_name,
away.team_name as awayteam_name
from
matches m,
teams home,
teams away
where
home.team_id = m.hometeam
and
away.team_id = m.awayteam
;
see SQLFIDDLE
I have the following tables
hobbies
+----+-------------+---------------+
| id | employeeid | hobby_name |
+----+-------------+---------------+
| 1 | 123 | cooking |
| 2 | 123 | painting |
| 3 | 124 | dancing |
+----+-------------+---------------+
nonacad_recog
+----+-------------+---------------+
| id | employeeid | recog_name |
+----+-------------+---------------+
| 1 | 123 | Award1 |
| 2 | 123 | Award2 |
| 3 | 124 | Award3 |
+----+-------------+---------------+
org_membership
+----+-------------+---------------+
| id | employeeid | recog_name |
+----+-------------+---------------+
| 1 | 123 | Boyscout |
| 2 | 124 | Girlscout |
+----+-------------+---------------+
This is the query I used:
SELECT h.hobby_name,r.recog_name,o.org_name
FROM hobbies as h
JOIN nonacad_recog as r ON (h.employeeid=r.employeeid)
JOIN org_membership as o ON (r.employeeid=o.employeeid)
WHERE h.employeeid='123'
I am getting duplicate outputs:
+----+-------------+---------------------+
| hobby_name | recog_name | org_name |
+----+-------------+---------------------+
| cooking | Award1 | Boyscout |
| cooking | Award2 | Boyscout |
| painting| Award1 | Boyscout |
| painting| Award2 | Boyscout |
+----+-------------+---------------------+
What I want as an output is like this: with no duplicates
+----+-------------+---------------------+
| hobby_name | recog_name | org_name |
+----+-------------+---------------------+
| cooking | Award1 | Boyscout |
| painting| Award2 | NULL |
+----+-------------+---------------------+
also giving a null to the other columns/rows if there is nothing to return.
I might have missed out some things.
Is this possible to achieve using mysql queries?
Any solution to this?
If I get the correct table, it will be easy for me to
mysqli_fetch assoc the results and display it on a table using PHP.
The problem is that in the desired output you want to relate rows in your tables by non-existent column, which is a row number.
You can technically do it like this
SELECT MAX(CASE WHEN source = 1 THEN name END) hobby_name,
MAX(CASE WHEN source = 2 THEN name END) recog_name,
MAX(CASE WHEN source = 3 THEN name END) org_name
FROM
(
SELECT 1 source, id, hobby_name name, #n1 := #n1 + 1 rnum
FROM hobbies CROSS JOIN (SELECT #n1 := 0) i
WHERE employeeid = 123
UNION ALL
SELECT 2 source, id, recog_name, #n2 := #n2 + 1 rnum
FROM nonacad_recog CROSS JOIN (SELECT #n2 := 0) i
WHERE employeeid = 123
UNION ALL
SELECT 3 source, id, org_name, #n3 := #n3 + 1 rnum
FROM org_membership CROSS JOIN (SELECT #n3 := 0) i
WHERE employeeid = 123
ORDER BY source, id
) q
GROUP BY rnum
Output:
| HOBBY_NAME | RECOG_NAME | ORG_NAME |
|------------|------------|----------|
| cooking | Award1 | Boyscout |
| painting | Award2 | (null) |
Here is SQLFiddle demo
Here is an alternative solution. You pack all values as delimited strings with GROUP_CONCAT()
SELECT GROUP_CONCAT(DISTINCT hobby_name ORDER BY h.id) hobby_name,
GROUP_CONCAT(DISTINCT recog_name ORDER BY r.id) recog_name,
GROUP_CONCAT(DISTINCT org_name ORDER BY o.id) org_name
FROM
(
SELECT 123 employeeid
) e LEFT JOIN hobbies h
ON e.employeeid = h.employeeid
LEFT JOIN nonacad_recog r
ON e.employeeid = r.employeeid
LEFT JOIN org_membership o
ON e.employeeid = o.employeeid;
Output:
| HOBBY_NAME | RECOG_NAME | ORG_NAME |
|------------------|---------------|----------|
| cooking,painting | Award1,Award2 | Boyscout |
Here is SQLFiddle demo
and then in your client php code easily explode() column values while iterating over the resultset and build your presentation.
I think "Limit" can work:
SELECT h.hobby_name,r.recog_name,o.org_name
FROM hobbies as h,
nonacad_recog as r,
org_membership as o
WHERE h.employeeid='123'
AND r.employeeid=o.employeeid
AND h.employeeid=r.employeeid
LIMIT 1
I'm a bit stuck with a php/Mysql Query. I got 2 tables :
table_users table_ranks
---------------- ------------------------
| id | points | | name | points_needed |
---------------- ------------------------
| 1 | 2 | | lvl0 | 0 |
| 2 | 10 | | lvl1 | 10 |
| 3 | 21 | | lvl2 | 20 |
| 4 | 29 | | lvl3 | 30 |
---------------- ------------------------
I need an ouput like this :
User_1 = lvl0 (because user has 2 points)
User_2 = lvl1 (because user has just reached 10 points)
...
User_4 = lvl2 (because user has not yet reached 30 points)
Think you :)
Regards.
You can do it like this
SELECT
tu.id,
tr.name,
tu.points
FROM table_ranks as tr
LEFT JOIN (SELECT * FROM table_ranks LIMIT 1,69596585953484) as l
ON l.points_needed = (SELECT MIN(points_needed) FROM table_ranks WHERE points_needed > tr.points_needed limit 1)
LEFT OUTER JOIN table_users AS tu ON tu.points >= tr.points_needed AND tu.points < l.points_needed
WHERE tu.id IS NOT NULL
group by tu.id
Fiddle
Output
-------------------------
| id | points | name |
-------------------------
| 1 | lvl0 | 2 |
| 2 | lvl1 | 10 |
| 3 | lvl2 | 21 |
| 4 | lvl2 | 29 |
-------------------------
give this a try, a little bit messy due to table design,
SELECT u.*, r.name
FROM table_users u
INNER JOIN
(
SELECT x.name,
x.points_needed start_point,
COALESCE(y.points_needed - 1, 2000000) end_point
FROM
(
SELECT name, points_needed, #rank:=#rank+1 ranks
FROM table_ranks a, (SELECT #rank:=0) b
ORDER BY points_needed
) x
LEFT JOIN
(
SELECT name, points_needed, #rank1:=#rank1+1 ranks
FROM table_ranks a, (SELECT #rank1:=0) b
ORDER BY points_needed
) y ON x.ranks+1 = y.ranks
) r ON u.points BETWEEN r.start_point AND r.end_point
SQLFiddle Demo