MySQL 2 counts in query [closed] - mysql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have this table, answers:
user
question
rightAnswer
ab
1
0
bc
1
1
de
1
1
bc
2
1
ab
2
1
de
2
0
fx
2
1
I want to create a view in phpMyAdmin that can display the question number, the total number of answers to that question and the number of right answers to that question.
I would like it to look like:
question
total answers
right answers
1
3
2
2
4
3
Could anyone help me to make this view? How do I create the query? I have tried something about count(*) where rightAnswer=1, but I cannot get it to count BOTH the total number of answers and the number of answers of each question.

try something like this:
select question,count(*),sum(rightAnswer)
from TABLE
group by question
order by question

select question, count(*) as total_answers, sum(rightAnswer) as right_answers
from your_table
group by question
order by question

Using ms sql syntax:
SELECT Question, totalAnswers.Total, rightAnswers.correct
FROM answers AS a
INNER JOIN (
SELECT Question, COUNT(rightanswer) AS correct
FROM answers
WHERE rightanswer = 1
GROUP BY Question
) rightAnswers
ON a.Question = rightAnswers.QUestion
INNER JOIN (
SELECT Question, COUNT(rightanswer) AS total
FROM answers
GROUP BY Question
) totalAnswers
ON a.Question = rightAnswers.QUestion

Related

Select 10 rows where sold='false' from SQL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm working on an IPN for PayPal, and I need to know how to select a certain amount of rows where sold='false'
The following should work:
SELECT * FROM your_table WHERE sold='false' LIMIT 10
select * from table where sold='false' limit 10
OR
select * from table where sold='false' limit M,N
this query fetches data starts from Mth row to N number of records
.
select top 10 * from table where sold='false'

Mysql GROUP_CONCAT and IN query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table EMPDetails like
EmpID EmpName EmpFriendsID
1 Hari 2,3
2 Ramesh
3 Suresh
I would like to have a query to retrieve EmpFriends name if i give an EmpID.
example if EmpID 1 is provided,result should be
1 Hari 2,3 Ramesh,Suresh
Thanks.
To Join tables use FIND_IN_SET() and then group recors and use GROUP_CONCAT() to concatenate friends names
SELECT t.EmpID,t.EmpName,t.EmpFriendsID,
GROUP_CONCAT(t1.EmpName)
FROM t
LEFT JOIN t as T1 on FIND_IN_SET(t1.EmpID,t.EmpFriendsID)
WHERE t.EmpID=1
GROUP BY (t.EmpID)
SQLFiddle demo
Use FIND_IN_SET() function
Try this:
SELECT E1.EmpID, E1.EmpName, GROUP_CONCAT(E2.EmpFriendsID)
FROM EMPDetails E1
LEFT JOIN EMPDetails E2 ON FIND_IN_SET(E2.EmpID, E1.EmpFriendsID)
GROUP BY E1.EmpID

Mysql query - grouping results [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have 2 tables and basically what I like to do is group the results or counts together for display. Tried different version of mysql statement but not getting anywhere.
The 2 example tables are:
tbl_One
index O_priority
1 low
2 medium
3 high
tbl_Two
t_priority
2
1
3
3
2
3
1
1
1
expected results:
low = 4
medium = 2
high = 3
SELECT T1.O_priority,T2.c FROM tbl_One as T1 LEFT JOIN (SELECT count(*) as c,t_priority FROM tbl_Two GROUP BY t_priority) as T2 ON T1.index = T2.t_priority;
Join the tables, then group the results:
SELECT tbl_One.O_priority, COUNT(*)
FROM tbl_One JOIN tbl_Two ON tbl_Two.t_priority = tbl_One.index
GROUP BY tbl_One.index
See it on sqlfiddle.
Simple as much as you can, try this:
SELECT count(o.index) as `index`, o.O_priority
FROM tbl_One o join tbl_two t on t.t_priority = o.index
group by t.t_priority;
SQL Fiddle

Replace multiple value in one field and value from other table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hello i have two table
table player
id Player Position
1 Messi 1,2,4
2 C.Ronaldo 1,2,3
3 Neymar 2,3
table position
id pos
1 CF
2 ST
3 WF
4 MF
and i want output like this
id player pos
1 Messi CF,ST,MF
2 C.Ronaldo CF,ST,WF
3 Neymar ST,WF
my sql fidle http://sqlfiddle.com/#!2/bf206/1
You really shouldn't use a comma separated field like this. Hard to read, inefficient and will cause major problems in the future.
However it is possible to do what you want like this, if the order of the positions for a player are not important:-
SELECT a.id, a.Player, GROUP_CONCAT(b.pos)
FROM player a
INNER JOIN position b
ON FIND_IN_SET(b.id, a.position) > 0
GROUP BY a.id, a.player
To keep the order you could try this (not tested):-
SELECT a.id, a.Player, GROUP_CONCAT(b.pos ORDER BY FIND_IN_SET(b.id, a.position))
FROM player a
INNER JOIN position b
ON FIND_IN_SET(b.id, a.position) > 0
GROUP BY a.id, a.player

Select Not Exist [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
this table called category
I want to select records with Sid=3 AND Bid =0 that not duplicated with Sid=3 AND Bid=8
(I want to select the red record in the image)
http://img27.imageshack.us/img27/8127/6opr.jpg
You can use a subquery like this :
SELECT * FROM Category WHERE Sid = 3 AND Bid = 0
AND Name NOT IN (SELECT Name FROM Category WHERE Sid = 3 AND Bid = 8)