i am developing an online test system in php,mysql..when a student submits a test its marks are calculated on the basis of correct answers matched from database using following query..
SELECT qa.question_id, qa.test_id, uta.user_answer,uta.user_id, qa.type,
qa.answers correct_answer,
CASE WHEN uta.user_answer = qa.answers THEN 'correct' ELSE 'incorrect' END
AS count(status)
FROM questions_answer qa
LEFT JOIN
(
SELECT user_id, type, test_id, question_id,
GROUP_CONCAT(answers ORDER BY answers) AS user_answer,
timestamp from user_test_answers
WHERE test_id = '1'
GROUP BY user_id, question_id
) uta
ON qa.question_id = uta.question_id
where qa.test_id=1 GROUP BY uta.user_id
my problem is:
I want to count the no of times "correct" appear in field STATUS field in table...
these no of correct answers are passed as an argument in a function.... i cant get results of other students....so i want to compare on the basis of CORRECT string appearing in field on the basis of user_id and display top 3 scorers.. How to do this "top 3 scorers in a particular test"..
I am a newbie to MySQL..
If I had to guess, the following should give you what you want. Change:
CASE WHEN uta.user_answer = qa.answers THEN 'correct' ELSE 'incorrect' END AS count(status)
to
sum(uta.user_answer = qa.answers) as NumCorrect
Related
I want make a leaderboard from my database, I already make a code but the sql says error
"SET #a1 = (SELECT SUM(d.poin_diskusi)
FROM diskusi d
GROUP BY d.id_akun)
MySQL said: Documentation
#1242 - Subquery returns more than 1 row"
heres my sql query :
SET #a1 = (SELECT SUM(d.poin_diskusi)
FROM diskusi d
GROUP BY d.id_akun);
SET #a2 = (SELECT SUM(j.nilai_jawaban)
FROM jawaban j
GROUP BY j.id_akun);
SET #hasil = #a1 + #a2;
SELECT #hasil
Thank you !
this is an example data in table jawaban:
this is an example data in table diskusi:
and my desired answer:
where tera123 have 135 TOTAL(d.poin_diskusi+j.nilai_jawaban) and david123 have 90 TOTAL(d.poin_diskusi+j.nilai_jawaban)
the point is to show TOTAL for every user
It looks to me (without looking at your images; if you want to show information, please provide it in text) like you want something like:
select id_akun, sum(point_value) as total_points from (
select id_akun, sum(poin_diskusi) as point_value
from diskusi group by id_akun
union all
select id_akun, sum(nilai_jawaban) as point_value
from jawaban group by id_akun
) as point_values
group by id_akun
The problem are the GROUP BY keywords. Inside the images you provided with example data, one can see that there are multiple entries for the columns jawaban.id_akun and diskusi.id_akun. Because of that, both SELECT will return multiple rows as result, for each different value of the id columns one summed up row. Try it without the GROUP BY clause.
This s my first question on SO, so please bear if I am not super clear! I am trying to sum up values of 2 columns from 2 tables. Both table values have a common ID called 'imid'. These imids are further divided as 'pmid' (say 1 imid can have one or multiple pmids). Both tables have different structures. I would want to sum values on 1 column from table1 and another column from table(to use it for a php calculation). When I try JOIN it always gives me a timeout error. Query below.
SELECT F.`imid` AS imid, SUM( F.Impression ) AS si, D.accmgr AS accmgr, D.cmname AS cmname,sum(D.Item_Impression_Cap) AS sim
FROM trasactions F, rawdata D
WHERE F.`imid` = D.`imid`
AND F.EntryDate LIKE '2017-%-%'
GROUP BY D.`imid`, F.`imid` ORDER BY F.`imid` ASC
I get results, but not even close to the correct numbers.(eg) Impression as 6,557,824 instead of 1,233,287 for a particular imid.
Not sure where I am wrong! Any help would be great...
Edit: Thanks for the responses...I managed to write the query...
SELECT F.`imid`, F.si , F.imname,D.Item_Start_Date ,D.sim, D.Item_End_Date, D.accmgr, D.cmname
FROM (SELECT `imid` AS imid, SUM(Impression ) AS si, adname AS imname, EntryDate FROM trasactions GROUP BY imid) F LEFT JOIN
(SELECT imid,Item_Start_Date, Item_End_Date, accmgr AS accmgr, cmname AS cmname, sum(Item_Impression_Cap) AS sim FROM rawdata GROUP BY imid) D ON F.`imid`=D.`imid`
WHERE D.cmname IS NOT NULL
GROUP BY F.`imid`
ORDER BY F.`imid` ASC
Now there is a new question! I am doing some calculations based on the array values derived from the query above...(eg) $pacing = $row['si']/$avgday*100;
Say I am listing the 'Pacing' for all items active. Would it be possible to count values from variables. (eg) Would want to show the count of items where $pacing is less than 100%. Would it even be possible to do that!! Thanks again.
I’m designing a program for my school to keep student attendance records. So far I have the following query working fine and now I would like to add an IF statement to perform a percentage operation when a certain condition is given. As it is, the query is using INNER JOIN to search for data from two different tables (oxadmain and stuattend) and it’s displaying the results well on a results table:
SELECT o.name
, o.year
, o.photoID
, o.thumbs
, s.ID
, s.studid
, s.date
, s.teacher
, s.subject
, s.attendance
FROM stuattend s
JOIN oxadmain o
ON s.studid = o.stuid
ORDER
BY name ASC
Now I would like to add an “if” statement that
1) finds when stuattend.attendance is = Absent, calculates the percentage of absences the students may have in any given period of time, and then stores that (%) value in “percentage” and
2) ELSE assigns the value of 100% to “Percentage”.
So far I’ve been trying with the following:
<?php $_GET['studentID'] = $_row_RepeatedRS['WADAstuattend']; ?>
SELECT oxadmain.name , oxadmain.year , oxadmain.photoID , oxadmain.thumbs , stuattend.ID , stuattend.studid , stuattend.date , stuattend.teacher, stuattend.subject , stuattend.attendance
CASE
WHEN stuattend.attendance = Absent THEN SELECT Count (studentID) AS ClassDays, (SELECT Count(*) FROM stuattend WHERE studentID = stuattend.studid AND Absent = 1) AS ClassAbsent, ROUND ((ClassAbsent/ClassDays)*100, 2) AS Percentage
ELSE
Percentage = 100
END
FROM stuattend INNER JOIN oxadmain ON stuattend.studid=oxadmain.stuid
ORDER BY name ASC
Any suggestions on how to do this well?
Thank you for your attention
The base idea would be:
select stuattend.studid, sum(stuattend.attendance = `absent`) / count(*)
from stuattend
group by stuaddend.studid;
This very much depends on exactly one entry per student and per day, and of course gets 0 if no absence and 1 if always absent.
To make this a bit more stable I would suggest to write a calendar day table, which simply keeps a list of all days and a column if this is a school day, so workday=1 means they should have been there and workday=0 means sunday or holiday. Then you could left join from this table to the presence and absence days, and even would give good results when presence is not contained in your table.
Just ask if you decide which way to go.
Hi I am new for developing.Kindly bear my codings. I have created a table arlog with id(auto increment), status, ticket number and code. Ticket and code number is set as unique. That is the duplicate of this combination cannot inserted again. But individually ticket number or cpt can be inserted as many times.It works fine. Now I want to use select query with another table with respect to the arlog table ticket number and code.Here is the select statement
$result = mysql_query("SELECT * FROM `ar` C WHERE provider='".$_SESSION['PROVIDER']
."' AND C.`TicketNo` IN ( SELECT TicketNo FROM `arlog` L where L.status NOT IN('New','Completed','Completed(Ar_aging)
','Completed(Rework)','Rework','Completed_Followup','Completed_Supervising' )
and L.assign='".$_SESSION['NAME']."' ) order by id desc") or die(mysql_error());
The query check the ticket number in arlog and displays correcly. But I want to combine TicketNo and Code in the arlog. I have made research but could not find solution. First of all is it possible?
Please try following sql:
SELECT L.TicketNo ,L.Code,C.* FROM `ar` C left join `arlog` L ON C.TicketNo = L.TicketNo where C.provider='your condition' and L.status NOT IN('New','Completed','Completed(Ar_aging)','Completed(Rework)','Rework','Completed_Followup','Completed_Supervising' ) and L.assign='your condition' order by by C.id desc
Hope this can help you!
I think you need to use CONCAT_WS()
There is a nice example of its usage in below link
MySQL SELECT AS combine two columns into one
This question already has an answer here:
Mysql Select some random rows and plus one specific row
(1 answer)
Closed 9 years ago.
Is it possible to get a specific value (like ID = 5) and in addition to that, two or three other random values from the same table?
Here's my query:
SELECT name, solution, MAX( solution )
FROM tracks
WHERE id !=5
GROUP BY name
ORDER BY RAND( )
LIMIT 0 , 30
What I want to do:
I have a quiz where I need to get three answers from the database. In my first query I'm getting the actual answer and in the second (the query above) I get the other two answer that are NOT the actual answer (ID != ).
The problem is, in my table, 4 values are the same, they have the same name, so if my acutal answer is ID = 5, there is the possibility that the query above will select the ID with 9 (it's the same name as ID = 5).
How can I avoid that?
Thank you!
Perhaps you could add a subquery to your where clause to lookup all solutions that are not the id and do not have the same solution name:
SELECT name, solution, MAX( solution )
FROM tracks
WHERE id !=5
--check that the id is not in the subquery and the name doesnt match:
AND id not in (select id from tracks t where id!=5 and t.name <> name)
GROUP BY name
ORDER BY RAND( )
LIMIT 0 , 30
Random results are random. If you want to ensure that you don't get results with the same name as the answer, you have to filter by name.
SELECT name, solution, MAX(solution)
FROM tracks
WHERE id != 5 AND name != (SELECT name FROM tracks WHERE id = 5)
GROUP BY name
ORDER BY RAND()
LIMIT 0 , 30
Yes, it's possible. Use UNION for that. (sqlfiddle here)
SELECT * FROM ( (
SELECT id, name, max(solution) as solution
FROM tracks
WHERE id = 5
GROUP BY name
LIMIT 0, 1
)
UNION (
SELECT id, name, solution
FROM tracks
WHERE id != 5
GROUP BY name
)) tmp ORDER BY RAND() LIMIT 0, 3