问题
MySQLfiddle here: http://sqlfiddle.com/#!2/6094e8/6
The fiddle contains the data that goes along with my examples below.
I have a single table I am trying to work with:
Table 1: user_answers table (stores users answers and their acceptable asnwers to various questions)
The notable values that are stored are:
users id (column uid
)
question id for the question they are answering (column quid
)
answer to the question (column answer
)
acceptable answer storage #1 stores other acceptable answers for that user (column acceptable_1
)
acceptable answer storage #2 stores other acceptable answers for that user (column acceptable_2
)
acceptable answer storage #3 stores other acceptable answers for that user (column acceptable_3
)
acceptable answer storage #4 stores other acceptable answers for that user (column acceptable_4
)
importance of their answer (column importance
)
The answer column will store a value between 1 and 4.
The acceptable columns hold selected acceptable answers which are in line with their column placement. For example, user 1 answers 1, but would accept answers 2 and 3. This would make acceptable_2 = 2
and acceptable_3 = 3
, leaving acceptable_1
and acceptable_2
with a value of 0.
For the sake of example I'll try and explain the results I am looking for:
Example 1:
User 1 answers 3 on quid 1 and would accept answers 2 and 4 from another user for that same question.
User 2 answers 2 on quid 1 and would accept answers 3 from another user for that same question
Result for Example 1: User 1 would accept user 2's answer and user 2 would accept accept user 1's answer. This result should be excluded from the results.
Example 2: User 1 answers 2 on quid 2 and would accept answers 1 or 4 from another user for that same question.
User 2 answers 2 on quid 2 and would accept answers 2 or 4 from another user for that same question.
Result for Example 2: User 1 would not accept user 2's answer, but user 2 would accept user 1's answer. This comparison has a single conflict in it and should be included in the results.
Example 3: User 1 answers 1 on quid 3 and would accept answers 1 or 2 from another user for that same question.
User 2 answers 1 on quid 3 and would accept answers 1 or 2 from another user for that same question.
Result for Example 3: User 1 would accept user 2's answer and user 2 would accept user 1's answer. There is no conflict and this result should be excluded from the results.
Example 4: User 1 answers 1 on quid 4 and would accept answers 1 or 2 from another user for that same question.
User 2 answers 3 on quid 4 and would accept answers 3 or 4 from another user for that same question.
Result for Example 4: User 1 would not accept user 2's answer and user 2 would not accept user 1's answer. There are two conflicts and this result should be included in the results.
As I hope is visible, I would like to grab the quid
for the questions both users have answered which have conflict in their answers in terms of what is in the other users acceptable columns.
So far I have been able to gather the answers and acceptable answers for each user using this query:
select ua.quid,
GROUP_CONCAT(IF(uid=1,answer,'') SEPARATOR '') as a1,
GROUP_CONCAT(IF(uid=2,answer,'') SEPARATOR '') as a2,
GROUP_CONCAT(IF(uid=1,acceptable_1,'') SEPARATOR '') as ac1_1,
GROUP_CONCAT(IF(uid=1,acceptable_2,'') SEPARATOR '') as ac2_1,
GROUP_CONCAT(IF(uid=1,acceptable_3,'') SEPARATOR '') as ac3_1,
GROUP_CONCAT(IF(uid=1,acceptable_4,'') SEPARATOR '') as ac4_1,
GROUP_CONCAT(IF(uid=2,acceptable_1,'') SEPARATOR '') as ac1_2,
GROUP_CONCAT(IF(uid=2,acceptable_2,'') SEPARATOR '') as ac2_2,
GROUP_CONCAT(IF(uid=2,acceptable_3,'') SEPARATOR '') as ac3_2,
GROUP_CONCAT(IF(uid=2,acceptable_4,'') SEPARATOR '') as ac4_2
from user_answers ua
where importance <> 1 and uid in (1, 2)
group by ua.quid
having sum(uid = 1) > 0 and
sum(uid = 2) > 0
Not being able to use the aliases I use to house the acceptable answers for each user in my WHERE clause (ac1_1, etc), I have yet to successfully compare all the variables to get the expected results.
Any guidance appreciated.
回答1:
You need something like this (using SELF JOIN
):
SELECT t1.quid, t1.uid As u1, t2.uid As u2
FROM user_answers t1
JOIN user_answers t2 ON t1.uid > t2.uid AND t1.quid = t2.quid AND
(FIELD(t1.answer, t2.acceptable_1, t2.acceptable_2, t2.acceptable_3, t2.acceptable_4) = 0 OR
FIELD(t2.answer, t1.acceptable_1, t1.acceptable_2, t1.acceptable_3, t1.acceptable_4) = 0 )
WHERE t1.importance <> 1 AND t2.importance <> 1 and t1.uid in (1, 2) AND t2.uid in (1, 2);
As you can see, question to answers comparison is done with FIELD function.
Here's your Fiddle with my code.
来源:https://stackoverflow.com/questions/20715005/convoluted-comparisons-and-exclusions-inside-a-single-mysql-table