brief overview so this is in context, I have an application in java that uses SQL as the database in the application users have to select 8 countries as answers to a question (these countries may be picked more than once, eg. Australia could be their answer for Q1 and Q3)
the country ID is then saved into their answers table alongside their user_id
i am trying to write a query that will get all of the country names that they chose as their answers, (i will then set the text of 8 labels in my java application so the user can see the 8 countries they selected as their answers)
i have no problem with SQL joins etc, the query i have written does pull the correct information however if someone has answered multiple question with the same answer(country) then it only returns the country names that were answers (so basically i want 8 returned but if the same country was selected twice it returns only 7) the issue with this is that different users could have answers different questions with the same country so i cannot simply use the answers returned and set 2 labels with the value from one of returned rows in the query (i hope that makes sense)
here is my SQL
SELECT C.C_NAME
FROM COUNTRY C INNER JOIN
TBL_ANSWERS T ON
T.ANSWER1_ID = C.C_ID
OR
T.ANSWER2_ID = C.C_ID
OR
T.ANSWER3_ID = C.C_ID
OR
T.ANSWER4_ID = C.C_ID
OR
T.ANSWER5_ID = C.C_ID
OR
T.ANSWER6_ID = C.C_ID
OR
T.ANSWER7_ID = C.C_ID
OR
T.ANSWER8_ID = C.C_ID
WHERE T.USER_ID = '4'
Im sure there is probably something rather easy i have missed out but any help would be much appriciated
also sorry if my question didnt fully make sense ill be happy to answer any questions you might have
thanks
It seems like you just want something like this:
However, I would seriously consider reworking your tables so that you use relationship mapping tables to figure out the questions and answers. This would allow this to be more easily created in one query
Something like
Tbl_Answer
Tbl_Question
This would allow you to just run a simple
BETWEEN. Something like this: