Go Premium for a chance to win a PS4. Enter to Win


Need to compare multiple columns against one another in MySQL table

Posted on 2015-01-04
Medium Priority
Last Modified: 2015-01-04

I have an online application I am building with PHP and MySQL where users register and are paired off with one another, after which they answer a series of questions on a scale of 1 - 5.  Only two users can be paired with one another and only their answers are to be compared.  Those answers are stored in a tabled called answers.  I have attached an image of the table structure, but here is below just in case; it is pretty simple:
 - answer_ID (primary key)
 - question_ID (foreign key that links to questions table)
 - user_ID (foreign key links to user table that tells what user answered the question)
 - answer (this is the answer value, always 1 - 5)
 - favorite (tag to highlight the answer for other purposes in the app)
 - discuss (tag to highlight the answer for other purposes in the app)

After users have answered questions they can go to a page to see how their answer's matched the user's they are paired with.  I have this page working as I want with all the queries except for the last scenario below.  My main desire is to have SQL do the lifting on matching results instead of using PHP programming to compare arrays.  

My goal: To have a query that shows all the questions (linked to questions table using foreign key in answers table: question_ID) where User-1 answered 1 and User-2 answered 4 or 5 when comparing answers to a specific question.  There will be other users answers in this table and I only want the query to consider User-1 and User-2.

I will always be comparing results where User-1 answered the question with a value of 1 and User-2 answered with a value of 4 or 5.  Questions can only be answered once and it is not a requirement that both users answer all questions in the application before the comparison is made; the query should just poll the answers in the table and match the question_ID column to find any results that should be returned.

Here are some scenarios and expected outcomes:
Q1)      IF  User-1 Answer Value =  1 and User-2 Answer Value = 5: Show question.
Q2)      IF User-1 Answer Value = 1 and User-2 Answer Value =  4: Show question.
Q3)      IF User-1 Answer Value = 1 and User-2 Answer Value =  2: Don’t show question.
Q4)      IF User-1 Answer Value = 3 and User-2 Answer Value = 3: Don’t show question.
Q5)      IF User-1 Answer Value =  3 and User-2 Answer Value =  4 or 5: Don’t show question.
Q6)      IF User-1 Answered Value = 5 and User-2 Answered Value = 4: Don’t show question.
Q2) IF User -1 Answer Value = 1 and User-5 Answer Value = 4: Don't show question.
Q7) IF User-1 Answer Value = 1 and User-2 has not answered: Don't show question.

So far everything I have tried just returns results where the answer was 1, 4 or 5 for either User-1 or User-2.  This is returning more results than is desired because I cannot figure out how to write the conditions where answer values for 1 are linked to User-1, then compared to User-2 answers with a value of 4 or 5 on the same question_ID.

I am not a complete newbie to the database game, but I am also not a full time developer / programmer either.  I have never tried to do anything like this and I am a bit lost.  I'm sure there is something pretty easy that I just don't have the experience or knowledge to know how to do.

Any help or recommendations would be great.  Thanks!
Question by:smauel76
  • 3
LVL 24

Accepted Solution

mankowitz earned 1500 total points
ID: 40530896
In brush strokes, you want to join the table with itself and proceed from there. It would be really useful to put up some sample data with your expected results. Start with this

select * from answers user1 join answers user2 on (user1.question_id=user2.question_id)
where user1.answer=1 and user2 >= 4
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40530932
I don't think your table structure makes any sense.  Note that int(5) means to display only 5 digits of a 32-bit integer.  It does not limit the amount of storage.  Same for tinyint(1) which is basically 0 thru 9.  http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html

Author Comment

ID: 40530956
Thanks for the quick replies.

Here is some sample data for you mankowitz, I attached an excel file as well as an image for reference.  This is the test data that is in the system from answers posted by test users.  All other queries work great except for this one I'm working on now.  The rows in yellow should return as results from the query that I can output to the user on the screen.  All other records should be ignored using this sample data.

To restate so you don't have to read back through my long winded initial post: the goal is to show all the records from the answers table where User-1 answered 1 to a question and User-2 answered 4 or 5 to the same question.

Thanks for taking a look at this.

Author Comment

ID: 40530995
I ended up getting it to work perfectly the way I need it to using the following query, thanks for the input guys! Thanks mankowitz for getting me going in the right direction. :)

SELECT answers.question_ID, answers.answer_ID, answers.user_ID, answers.answer, questions.question, category.category, user.user_ID, user.spouse_ID, AES_DECRYPT(user.fname, 'Eph521')
FROM questions
INNER JOIN answers ON questions.question_ID = answers.question_ID
INNER JOIN category ON questions.category_ID = category.category_ID
INNER JOIN user ON answers.user_ID = user.user_ID
WHERE (answers.user_ID = '2' AND answers.answer = '1') OR (answers.user_ID = '1' AND (answers.answer = '4' OR answers.answer = '5'))
GROUP BY question_ID HAVING COUNT(1) > 1 ORDER BY category DESC

Author Closing Comment

ID: 40530996
mankowitz got me pointed in the right direction focuses back on doing a simple join rather than trying to do a UNION to compare the table with itself.  Very helpful.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question