Mysql query - grouping results [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 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

Related

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 2 counts in query [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 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

SQL Query group shuffle [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 8 years ago.
Improve this question
I have a query like
select id, item, producer from table
The result is like this:
id item producer
1 apple A
2 pear A
3 peach A
4 orange B
5 strawberry B
6 melon B
I want to shuffle this result (order by id DESC) and get something like this
item producer
strawberry B
pear A
orange B
apple A
peach A
melon B
I DON'T want to display like this:
ALL A ITEM
ALL B ITEM
ALL C ITEM...
Use the rand function in ORDER BY like this:
select id, item, producer from table order by rand();
To Shuffle the selection you can use rand()
The Answer for the link below contains more information.
SELECT id, item, producer
FROM table
ORDER BY RAND()
MySQL: Shuffle a limited query result?
select id, item, producer from table order by rand()
Use Order BY rand() to randomly order the results

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 couple of random rows by different criteria in one 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 want to select 4 random rows from a table where level = 1 and 4 random rows from the same table where level=2. How can I do this in 1 query ?
select * from (select * from your_table
where level = 1 order by rand() limit 4) x
union all
select * from (select * from your_table
where level = 2 order by rand() limit 4) y
try like this...
SELECT product_id, title, description FROM products WHERE active = 1 AND stock > 0 ORDER BY RAND() LIMIT 4;