Intersect multiple rows of the table based on conditions [duplicate] - mysql

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

Related

Select top first row for each group based on multiple order criterias [duplicate]

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
)

how use having count (distinict (col1, col2 )) >2 to get records fulfill any 2 or more conditions

How can I use HAVING COUNT (DISTINCT (col1, col2)) >2 in sql to find out those users who have any 3 following records or more
SELECT user_id FROM users_responses
WHERE
(question_id = 3 AND response_id = 6)
OR(question_id = 7 AND response_id = 18)
OR(question_id = 1 AND response_id = 5)
OR(question_id = 8 AND response_id = 22)
OR(question_id = 2 AND response_id = 3)
HAVING COUNT(DISTINCT question_id, response_id) > 2 ;
I have total 10 different users in this table with same and different responses. 3 users have exact responses are given above and 3 users fulfill 4 conditions while others users have only1-2 same responses given in query.
I need to fetch all those distinct user_id from this table who have responded to above question id and have same responses.
I tried but it gives only first record
Table structure
Id | user_id | question_id | response_id
Any idea how to make changes to get it work properly.

Return result depend on conditions for multible rows without self join

I have a large answers table like this
person_id|question_id|answer
1|101|6
1|102|2
1|103|5
2|101|2
2|102|5
2|103|5
3|101|2
3|102|8
3|103|6
4|101|2
4|102|8
4|103|6
4|101|6
4|102|2
4|103|5
How to return persons depend on multiple question answers? For example I need to return person who answers:
6 for question 101
and 2 for question 102
and 5 for question 103
the query should return person 1 and 4
and consider that I need to filter depend on 10 questions, so I don't need to do 10 self join on the table :)
You can do this using group by and having:
select person_id
from t
where (question_id, answer) in ( (101, 6), (102, 2), (103, 5) )
group by person_id
having count(distinct question_id) = 3;
Note that the "3" needs to match the number of questions you have in the in list.
Try this :
SELECT DISTINCT PersonID
FROM TableName
WHERE 1 = CASE WHEN question_id = 101 AND Ans=6 THEN 1
WHEN question_id = 102 AND Ans=2 THEN 1
WHEN question_id = 103 AND Ans=5 THEN 1
END
SELECT PersonID
FROM (
SELECT PersonID,
CASE WHEN question_id = 101 AND Ans=6 THEN 1
WHEN question_id = 102 AND Ans=2 THEN 1
WHEN question_id = 103 AND Ans=5 THEN 1
END as [count]
FROM TableName
GROUP BY PersonID,question_id,Ans
) x
WHERE x.[count]=3

MySQL: update by selection condition

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)

Select result from table for matching 2 rows in mysql

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)