Mysql select records from 2 rows - mysql

I have a table include some data like that. This is example for question/answer script. ID is auto increment and if PID=0 then it is question, when reply any question then PID is set to question's ID. There is no Subject for replies.
ID PID SUBJECT CONTENT DATE
1 0 First Question This is my first 09/01/2013
2 0 Second Question This is second 09/01/2013
3 1 Yes this is first 09/01/2013
4 2 I agree this is second 10/01/2013
5 0 Third Question This is third question 11/01/2013
6 1 Reply to first 11/01/2013
7 1 Another reply to first 12/01/2013
8 5 This is reply of 5th 13/01/2013
9 2 Last try for second 14/01/2013
My questions are,
How can I select questions with reply count?
Ex.
First Question (3)
Second Question (2)
Third Question (1)
How can I select today's answered questions or answers?
Ex. For 09/01/2013
First Question (2) ---> 1 question and 1 answer but 2 actions
Second Question (1) ---> just 1 question

Select the questions and join against the answers:
select q.id, q.subject, count(a.id)
from yourtable q
left join yourtable a on q.id=a.pid
where q.pid=0
group by q.id;

Try join for first task
SELECT
q.id as ID,
q.pid as PID,
q.subject as SUBJECT,
COUNT(lq.id) as Total
FROM questions as q
LEFT JOIN questions as lq ON lq.pid = q.ID
WHERE q.PID = 0
GROUP BY q.id
OUTPUT
ID PID SUBJECT TOTAL
1 0 First Question 3
2 0 Second Question 2
5 0 Third Question 1
Demo
EDITS :
For second part. You should note that there can be many other ways for doing the same task.
SELECT
q.id as ID,
q.pid as PID,
q.subject as SUBJECT,
(COUNT(lq.id) - 1) as Total,
q.date
FROM questions as q
LEFT JOIN questions as lq ON lq.pid = q.ID OR lq.id = q.PID
WHERE q.date = DATE(NOW())
OUTPUT
ID PID SUBJECT TOTAL DATE
1 0 First Question 2 January, 09 2012 00:00:00+0000
2 0 Second Question 1 January, 09 2012 00:00:00+0000
Demo

For question 1
select PID,count(*) from table
where pid<>0
group by PID
For question 2
select PID,count(*) from table
where pid<>0 and date=current_date()
group by PID

Related

How create sum from tabel 2 and show data tabel 1 and tabel 2 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Tabel A :
id
invoice
1
001
2
002
Tabel B :
id
invoiceId
price
1
1
10
2
1
20
3
1
10
4
2
15
Expect
id
invoice
total
count
1
001
10
40
2
001
20
40
3
001
10
40
4
002
15
15
my relation one to many.
because i need one column with name count base on tabel B sum(price) with group by invoiceId
How to make it mysql query above expect from tabel A and B ?.
On MySQL 8+, we can handle this without a subquery by using analytic functions:
SELECT
b.id,
a.invoice,
b.price AS total,
SUM(b.price) OVER (PARTITION BY a.id) AS count
FROM TableB b
LEFT JOIN TableA a
ON a.id = b.aid;
On earlier versions of MySQL, or if you are doing this from an ORM layer which doesn't like analytic functions, we can also try using a join to aggregate the counts:
SELECT
b1.id,
a.invoice,
b1.price AS total,
COALESCE(b2.count, 0) AS count
FROM TableB b1
LEFT JOIN TableA a
ON a.id = b1.aid
LEFT JOIN
(
SELECT aid, SUM(price) AS count
FROM TableB
GROUP BY aid
) b2
ON b2.aid = b1.aid;

sql select with count condition

I have 2 tables in my DB, first table called "questions" and hold questions with id, second table called "answers" and hold answers for the questions (as multiple choices).
how to select questions that have less than 4 answers?
questions table:
id question
1 what is ...?
2 how many ...?
3 Is ....?
answers table
id question_id answer
1 1 54
2 1 11
3 1 22
4 2 England
5 1 5
6 2 Turkey
how to select questions that have answers less than 4?
thanks,
select questions.id, questions.question from questions
inner join answers on questions.id = answers.question_id
group by questions.id, questions.question having count(questions.id) <4
here you go.
use this :
SELECT *
FROM questions
WHERE id in
(
SELECT question_id
from answers
group by question_id
having count(question_id) <4
)

Limit each group in group by

Now I understand that this has been asked several times before, but I have tried to apply different existing solutions to my specific problems for quite a while without success. So I turn here in hope of some guidance.
I have a table called tblanswers, which contains answers linked to different questions in another table. What I want is to get the count for each answer for a specific question ID, but limit it to the n first answers each month.
Sample data from tblanswers:
id qid answer timestamp
72 162 2 1366027324
71 161 4 1343599200
70 162 2 1366014201
69 161 4 1366011700
68 162 2 1366006729
67 161 3 1366010948
66 162 2 1365951084
This is the query I have so far:
SELECT *, COUNT(*) c FROM(
SELECT answer, timestamp, YEAR(FROM_UNIXTIME(timestamp)) yr, MONTH(FROM_UNIXTIME(timestamp)) mo FROM tblanswers
WHERE qid = 161
ORDER BY timestamp ASC
) q GROUP BY YEAR(FROM_UNIXTIME(timestamp)), MONTH(FROM_UNIXTIME(timestamp)), answer
That would give me something like this: (the dates and numbers in sample data is not accurate)
answer yr mo c
1 2013 5 5
2 2013 5 3
3 2013 5 2
1 2013 6 5
2 2013 6 15
3 2013 6 7
Let's say I only want to see the first three answers in a month, then count could never be more than 3. How can I limit each month?
The final data should be a sum of each answer, like this:
answer num_answers
1 2
2 3
3 3
I think one of these solutions could work, but not how:
http://code.openark.org/blog/mysql/sql-selecting-top-n-records-per-group
http://code.openark.org/blog/mysql/sql-selecting-top-n-records-per-group-another-solution
Any help is appreciated. Thanks!
This solution is based on the top-N-per-group method here
SELECT answer, COUNT(*) num_answers
FROM (SELECT answer, yearmonth,
#rn := CASE WHEN #prevmonth = yearmonth
THEN #rn + 1
ELSE 1
END rn,
#prevmonth := yearmonth
FROM (SELECT #rn := NULL, #prevmonth := NULL) init,
(SELECT answer,
YEAR(FROM_UNIXTIME(timestamp))*100+MONTH(FROM_UNIXTIME(timestamp)) yearmonth
FROM tblanswers
WHERE qid = 220
ORDER BY timestamp) x) y
WHERE rn <= 3
GROUP BY answer
SQLFIDDLE
What about this solution:
SELECT qid, answer, YEAR(FROM_UNIXTIME(timestamp)) yr, MONTH(FROM_UNIXTIME(timestamp)) mo, COUNT(*) no
FROM tblanswers
WHERE qid = 161
GROUP BY answer, yr, mo
HAVING COUNT(*) <= 2
ORDER BY timestamp ASC;
and the fiddle: http://sqlfiddle.com/#!2/1541eb/126
There is no reason to reinvent a wheel and risk you have a buggy, suboptimal code. Your problem is trivial extension of common per group limit problem (see also tag limit-per-group). There are already tested and optimized solutions to solve this problem.

Selecting id and value where max occurs

I've solved one issue and ran into another. Basicaly i want to select question_id, answer and maximum number of occurences. I run my query from basic table that gathers questions and answers to them (question id represents question and answer represents answer from 0 to 5 that corresponds to other table but that doesn't matter).
**survey_result**
question_id
answer (int from 0 to 5)
Sample survey_result:
question_id answer
1 3
1 5
1 2
2 2
2 0
2 4
Here's the query, it's purpose is to check for every single question, which answer (from 0 to 5) occured the most.
select question_id, answer, max(occurence_number) FROM
(select question_id, answer, count(*) as occurence_number
from survey_result
group by question_id, answer
order by question_id asc, occurence_number desc) as results
GROUP BY question_id
So a sub query results in something like this:
question_id answer occurence_number
1 0 12
1 1 20
1 2 34
1 3 5
1 4 9
1 5 15
But main query results something like this:
question_id answer occurence_number
1 0 12
2 0 20
3 0 34
4 0 5
So the problem is that it always shows answer 0, and i want to get correct answer number.
Sadly a bit redundant due to MySQL's lack of a WITH statement, but this should do what you want. In case of a tie, it will return the higher answer.
SELECT s1.question_id, MAX(s1.answer) answer, MAX(s1.c) occurrences
FROM
(SELECT question_id, answer, COUNT(*) c
FROM survey_result GROUP BY question_id,answer) s1
LEFT JOIN
(SELECT question_id, answer, COUNT(*) c
FROM survey_result GROUP BY question_id,answer) s2
ON s1.question_id=s2.question_id
AND s1.c < s2.c
WHERE s2.c IS NULL
GROUP BY question_id
An SQLfiddle to play with.
I think you are overcomplicating it, try this:
select question_id, answer, count(*) as occurence_number
from survey_result
group by question_id, answer

MySQL query: select how many times was every option selected

I have 2 mysql tables
1. questions: with the following columns: id, title, answer1, answer2, answer3, answer4, answer5, nranswers.
and
2. answers with the following columns: id, questionid, userid, answer
Every question has maximum 5 answers( it can have between 2 and 5 answers). My problem is that I want to select from my database, for a given question, how many times was every option selected.
For example, let's suppose I have the question with the id 46, with 4 answers, and 48 users voted for the option #2, 37 users for the option #1 and 39 for the option #4.
I want a query that selects that and write these things:
1 37
2 48
3 0
4 39
P.S. VERY IMPORTANT! IT MUST COUNT ONLY NRANSWERS ANSWERS, AND IT MUST ECHO THE ONES THAT WEREN'T VOTED BEFORE.
Best way to do this: change table defs:
Questions (Question_ID, title)
Answers (Answer_ID, Question_ID, answer_text)
Votes (User_ID, Answer_ID)
Which contains the same data as your def, but is in first normal form. Selecting the counts is now really easy
SELECT
a.Answer_ID,
COUNT(v.User_ID)
FROM
Questions q
LEFT JOIN Answers a ON q.Question_ID = a.Question_ID
LEFT JOIN Votes v ON a.Answer_ID = v.Answer_ID
WHERE q.Question_ID = 46 -- or any other question ID
GROUP BY a.Answer_ID
ORDER BY a.Answer_ID;
SELECT q.id as question, a.answer as answer, count(a.answer) as count FROM questions q, answers a Group by q.id,a.answer
Problem with above that it will return as follows
question answer Count
1 1 37
1 2 48
1 4 39
1 3 0 this is missing
OR
SELECT a.question_id, a.answer, count(a.answer) FROM test.answers a Group by a.question_id, a.answer