I have system of questions and answers,
each question should have 4 answers
I need to run SQL query to update question's "class" to be "D" (to prevent it from appear) if it has less than 4 answers
tables structures:
question (table)
id text class
1 Is.. A
2 Where.. B
3 where.. A
4 Why.. A
5 Do.. A
answer (table)
id qid text
1 1 ..
2 1 ..
3 1 ..
4 2 ..
5 3 ..
note: "answer.qid" refer to "question.id"
thanks,
Try something with subquery like:
UPDATE question
SET class = 'D'
WHERE id IN
(SELECT qid
FROM answer
GROUP BY qid
HAVING count(qid) < 4)
On the fly I would say, you could use a subselect:
UPDATE question SET class='D' WHERE ( SELECT count(*) FROM answer INNER JOIN answer.qid = question.id ) < 4;
update question
set class='D'
where question.id in (select qid from answer group by qid having count(*)<4)
Related
This question already has answers here:
Select first row in each GROUP BY group?
(20 answers)
Closed 3 years ago.
Disclaimer: I have searched for similar questions but I haven't found a clear answer to my issue.
I have a table like this:
id | catid | views | text
1 100 2000 "sometext"
2 200 2000 "sometext"
3 200 3000 "longertext"
For each catid (that in this case are just 2: 100 and 200) i need to get the record with the most views and the longer texts... In this case the result would be:
id | catid | views | text
1 100 2000 "sometext"
3 200 3000 "longertext"
With priority on the number of views.
I have tried some queries with inner joins but none seems clear and working...
Any thoughts ?
A method uses a correlated subquery with order by and limit for filtering. Assuming that id is primary or unique key:
select t.*
from mytable t
where t.id = (
select id
from mytable t1
where t1.catid = t.catid
order by t1.views desc, char_length(t1.text) desc
limit 1
)
I have a pretty simple table like this:
id | custom_id | answer
----------------------------------
1 | 123 | Answer 1
__________________________________
2 | 123 | Answer 2
__________________________________
3 | 422 | Answer 3
__________________________________
4 | 345 | Answer 2
__________________________________
5 | 992 | Answer 1
__________________________________
6 | 452 | No answer
__________________________________
What I am trying to do is count the number of Answer 1, Answer 2, etc. So, for the above data I would expect to get:
2 * Answer 1
2 * Answer 2
1 * Answer 3
Note, that anything that is No answer should be discarded.
However, further to the above, I want to take into account only one answer per custom_id, and this should be their first answer. So really, the output I expect to get for the above data should be:
2 * Answer 1
1 * Answer 2
1 * Answer 3
This is because we take only the first answer for custom_id = 123.
So far, I have made the following query:
select
answer,
count(*) as totalCount
from
" . DB_TABLE . "
where
answer <> 'No answer'
group by
custom_id
However, this seems to return the total counts (as I explained first), not taking into consideration that there should only be one per custom_id. I thought the group by would solve this issue, but this does not seem to be the case.
How can I achieve the results I am after?
Thanks
One approach, will be first to create a derived table with the IDs of the first answers for every custom_id and also filter those with values No answer (since you want to ignore they), like this:
SELECT
custom_id,
MIN(id) AS firstAnswerID
FROM
<table_name>
WHERE
answer <> "No Answer"
GROUP BY
custom_id
Then, we can join the original table with this previous one on the ID column (this will act like a filter for those that aren't first answers or have the No answer value), make a GROUP BY the answer column and count the numbers of each one. In summary, this will do what you want:
SELECT
t1.answer,
COUNT(*) AS NumTimes
FROM
<table_name> AS t1
INNER JOIN
( SELECT
custom_id,
MIN(id) AS firstAnswerID
FROM
<table_name>
WHERE
answer <> "No Answer"
GROUP BY
custom_id ) AS t2 ON t2.firstAnswerID = t1.id
GROUP BY
t1.answer
ORDER BY
NumTimes DESC
You can play with this here: DB Fiddle
Try to use this:
select answer, count(answer) as totalCount from " . DB_TABLE .
" where answer <> 'No answer' group by answer
You should count rows for every answer group, not for the entire table.
This question already has answers here:
Alternative to Intersect in MySQL
(9 answers)
Closed 4 years ago.
I have a table with the columns response_id, question_id and answer_id.
I would like to find all response_id matching multiple conditions. For example the following are some usecases
User selected answer 1 for question 1 and answer 2 of question 2
User (selected answer 3 for question 1 and answer 2 for question 2) or answer 1 for question 3
In SQL, I can do this using INTERSECT and UNION but the INTERSECT is not available in MySQL. Can someone guide me how to solve this problem?
example in SQL, which is needed in MySQL.
select distinct(response_id) from table where question_id = 873 AND answer_id = 5269
intersect
select distinct(response_id) from table where question_id = 874 AND answer_id = 5273
intersect
select distinct(response_id) from table where question_id = 877 AND answer_id = 5286
MySQL does not support INTERSECT, but we may simulate it using an EXISTS clause:
SELECT DISTINCT response_id
FROM table
WHERE
question_id = 873 AND
answer_id = 5269 AND
response_id IN (
SELECT DISTINCT response_id FROM yourTable
WHERE question_id = 874 AND answer_id = 5273) AND
response_id IN (
SELECT DISTINCT response_id FROM yourTable
WHERE question_id = 877 AND answer_id = 5286);
SELECT DISTINCT * FROM
(SELECT table.response_id FROM table, data WHERE table.question_id = 873 AND
table.answer_id = 5269) query1
INNER JOIN
(SELECT table.response_id FROM table, data WHERE table.question_id =874 AND
table.answer_id = 5273) query2
I have a MySQL table has these fields
ID, CID, QUESTION, ANSWER, USER
ID is auto increment, every record in table has ID;
CID is point to ID for ANSWER records
For example, we have 4 records, 2 of question 2 of answer, and Mike answers 2 questions
ID CID QUESTION ANSWER USER
1 0 Test NULL John
2 1 NULL This is Test Mike
3 0 Example NULL Tracy
4 3 NULL Yes it is Mike
I want to list questions of which is Mike's answers. How can I match ID and CID fields in same table and print QUESTION for output
I want to list questions of which is Mike's answers.
SELECT t1.*
FROM TableName t1
LEFT JOIN TableName t2 ON t1.ID = t2.CID
WHERE t2.Answer IS NOT NULL
AND t2.User = 'Mike';
SQL fiddle Demo
Note that this gives you the list of questions that Mike has answered therefore you won't find mike listed on them:
ID CID QUESTION ANSWER USER
1 0 Test NULL John
3 0 Example NULL Tracy
select QUESTION from yourtable
where ID in(select ID from yourtable where User = 'Mike' and answer is NOT NULL)
I have a table similar to below
Questions table:
Qid Tags
--- ---------
1 SQL
1 DATABASE
1 ALGORITHM
2 ALGORITHM
2 TAGS
3 SQL
3 SYNTAX
3 DATABASE
The following
SELECT * FROM Question table WHERE TAGS='SQL'
will display:
Qid Tags
--- ----
1 SQL
2 SQL
3 SQL
I'm looking for a query which will display the highest related question based on similar tags.
So if I looked up Question table - Qid=1. The result would be Qid 3, as Qid 1 and Qid 3 have 2 similar terms, but Qid 2 only as 1 similar tag to Qid 1. If i looked up Qid 2 it would return Qid 1 as it they share 1 tag and Qid 2 and 3 share no tags. And If i looked up Qid 3 it would return Qid 1 as it as Qid 1 has 2 tags that Qid 3 has, but Qid 2 has no tags that Qid 3 has.
Thanks for any help.
Something along these lines maybe?
SELECT Qid, COUNT(*) AS TagsInCommon FROM Questions
WHERE Tags IN (
SELECT DISTINCT Tags FROM Questions WHERE Qid=1
)
GROUP BY Qid
ORDER BY TagsInCommon DESC
I'm not sure about the field names but you get the idea...
declare #Qid int
set #Qid = 1
select top 1 Q2.Qid, COUNT(*) from Questions Q1
inner join Questions Q2 on Q1.Tags = Q2.Tags
where Q1.Qid = #Qid and Q2.Qid <> #Qid
group by Q2.Qid
order by COUNT(*) desc