SQL Query creation with multiple joins - mysql

I am working with MYSQL, and want to create a query. I have 3 tables.
QUESTION
QUESTION_NUMBER
QUESTION_DESCRIPTION
1
anydesc
2
anydesc2
ANSWER
ANSWER_NUMBER
ANSWER_DESCRIPTION
1
anydescANS
2
anydesc2ANS
3
anydesc3ANS
And a bridge TABLE
QUESTION_ANSWER
Q_NUM
A_NUM
1
1
2
2
2
3
Now I want to retrieve all the Answers of let's say a specific QUESTION NUMBER i.e 1.
What I have tried.
SELECT QUESTION.QUESTION_NUMBER, QUESTION.QUESTION_DESCRIPTION, ANSWER.ANSWER_NUMBER, ANSWER.ANSWER_DESCRIPTION
FROM QUESTION
RIGHT JOIN QUESTION_ANSWER ON QUESTION.QUESTION_NUMBER=4
INNER JOIN ANSWER ON QUESTION_ANSWER.Q_NUM=4
and similar queries, but I can not make sense of it, and can not get desired output.
What I want is to get all the answers that belong to specific Question Number.

I would write it this way:
SELECT
Q.QUESTION_NUMBER,
Q.QUESTION_DESCRIPTION,
A.ANSWER_NUMBER,
A.ANSWER_DESCRIPTION
FROM QUESTION AS Q
INNER JOIN QUESTION_ANSWER AS QA ON Q.QUESTION_NUMBER = QA.Q_NUM
INNER JOIN ANSWER AS A ON QA.A_NUM = A.ANSWER_NUMBER
WHERE Q.QUESTION_NUMBER=4;
You don't need RIGHT OUTER JOIN, you need INNER JOIN, because there is certain to be a matching question if you are seeking answers.
I recommend avoiding the "comma-style" join syntax. That was made obsolete in 1989. It works the same as inner join, but it's worth using the modern syntax consistently.

This should work for your purpose:
SELECT q.question_number, q.question_description,
a.answer_number, a.answer_description
FROM question q, answer a, question_answer qa
WHERE q.question_number = qa.q_num
AND a.answer_number = qa.a_num
AND q.question_number = 2

Related

how to join two tables when matching entry doesn't exist [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I am having some trouble writing this SQL Query.
Basically, I have the following tables;
received_flight_files_table & uploaded_flight_files_table
And the following fields;
Processing_Month, IATA_Code, Airline, Received_Date, Uploaded_Date
I would like the query to produce something like the first attached screenshot.
The 2 date fields you can see are from different tables though.
The join can be done on the IATA_Code and Airline fields as both of these values should be the same in both tables.
Here are 2 examples of statements I have written which aren't quite right;
SELECT DISTINCT received_flight_files_table.Processing_Month, received_flight_files_table.IATA_Code, received_flight_files_table.Airline, received_flight_files_table.Received_Date, uploaded_flight_files_table.Uploaded_Date, published_flight_table.Published_Date
FROM ((received_flight_files_table
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.Processing_Month=received_flight_files_table.Processing_Month)
INNER JOIN published_flight_table ON published_flight_table.Published_Month=received_flight_files_table.Processing_Month
WHERE received_flight_files_table.Processing_Month = [enter MMMYY];`
SELECT DISTINCT received_flight_files_table.Processing_Month, received_flight_files_table.IATA_Code, received_flight_files_table.Airline, received_flight_files_table.Received_Date, uploaded_flight_files_table.Uploaded_Date
FROM (((received_flight_files_table
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.Processing_Month=received_flight_files_table.Processing_Month
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.IATA_Code=received_flight_files_table.IATA_Code
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.Airline=received_flight_files_table.Airline)))
WHERE (received_flight_files_table.Processing_Month = [enter MMMYY];
There should sometimes be some blanks in the Uploaded_Date field as for example - in one table I might have received the files but not yet uploaded them.
Try to use LEFT JOIN instead of INNER JOIN.
https://www.diffen.com/difference/Inner_Join_vs_Outer_Join

SQL problem with JOIN statement (multiple results from 1 table)

So I have a SQL query that selects quiz_questions and quiz_question_options
But when I run my SQL query I only get 1 result from quiz_option (in my database I have 3 options for each question). How would I be able to display all 3 options for every question?
Tables:
quiz_question
-id (PK)
-quiz_id(FK)
-question
quiz_question_option
`-id (PK)
-quiz_question_id(FK)
-quiz_option`
This is my code so far:
SELECT quiz_question.question,quiz_question_option.quiz_option
FROM quiz_question_option
LEFT JOIN quiz_question
ON quiz_question.id = quiz_question_option.quiz_question_id
ORDER BY RAND() LIMIT 5
The limit will change depending on user input
EDIT:
The result i get is:
question |quiz_option
1.question|1.answer
2.question|2.answer
What I need to get is:
question |quiz_option
1.question|1.answer
1.question|2.asnwer
1.question|3.answer
2.question|1.answer
2.question|2.answer
3.question|3.answer
etc...
2.EDIT:
I need to make a sql query for my project (website)
so the user will select how many questions he wants to have.
On the next page it would display the questions. I already have the code to display random questions. But I don't know how to display the options for the selected question.
seems you want to ignore the relationship. what you want to achieve is something like cross join
select t1.question, t2.quiz_option
from quiz_question_option t1
cross join quiz_question t2
I believe INNER join would help you.
Try this:
SELECT quiz_question.question, quiz_question_option.quiz_option
FROM quiz_question_option AS O
INNER JOIN quiz_question AS Q
ON Q.id= O.quiz_question_id

how to join two table with where to one column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
can anyone teach me how to join two tables into one column MySQL
and also is that possible? or not?
If you need a result as a single column you could use concat
SELECT concat(p.id, p.status, p.date, m.pid)
FROM posts p
INNER JOIN post_meta m ON m.pid=p.id
WHERE (status=1 OR status=4)
ORDER BY date DESC
But you need explicit column (all you need) but not select *
Or could be you are looking for group_concat
SELECT group_concat(p.title)
FROM posts p
INNER JOIN post_meta m ON m.pid=p.id
WHERE (status=1 OR status=4)
Yes. Absolutely, join the tables together in SQl(MySql/SqlSvr) is possible.
(INNER) JOIN: Select records that have matching values in both tables.
LEFT (OUTER) JOIN: Select records from the first (left-most) table with matching right table records.
RIGHT (OUTER) JOIN: Select records from the second (right-most) table with matching left table records.
FULL (OUTER) JOIN: Selects all records that match either left or right table records.
All INNER and OUTER keywords are optional.
Let read more about this tutorial.
https://www.dofactory.com/sql/left-outer-join
Appears to be repeat of How can I merge the columns from two tables into one output?
Provide more details on how you want to join for complete response. An example will be better with input and expected output.
Simple answer
Select * From table1 t1 INNER JOIN table2 t2 on t1.col1 = t2.col1;
You can use INNER for getting result where data should be present on both table corresponding to col1
You can use LEFT for getting result where data should be present on first table corresponding to col1 but not necessarily present on second table
You can use RIGHT for getting result where data should be present on second table corresponding to col1 but not necessarily present on first table

Selecting multiple values from multiple tables

MySQL.
I have two tables, one is "Questions" and the other is "Answers"
The Questions table:
- question_id
- user_id
- question
The Answers table:
- answer_id
- question_id
- user_id
- answer
- correct
The goal is to get all questions (and associative answers) based on a user's id. I've been able to get all of the answers, however I'm only getting one question. I can see why it's only getting a single question, but I don't have any idea how to go about getting the question text for each answer.
Here's the code that I'm using right now. Where id_in is an input value on a saved procedure. The issue is that it gives me all of the answers for each question, but all of them return the same question text. I feel like possibly a type of join would be better here, but we haven't started learning about them yet and I hardly know anything about them as is.
BEGIN
DECLARE question_text VARCHAR(40);
SELECT question INTO question_text FROM questions WHERE user_id = id_in;
SELECT question_text, Q.* FROM answers AS Q WHERE user_id = id_in;
END
Yes, this is homework. I'm just completely lost as to what I need to be doing.
Left joins allow for All things in the left table, and only the matching things in the right table. In my example I may have A and Q mixed up but I think this is the general gist of it. You can also take the user_id = in_id and move that to a wear, but filter on the join should be faster.
SELECT
Q.QUESTION
, A.ANSWER
, A.CORRECT
FROM ANSWERS A
LEFT JOIN QUESTION Q
ON A.QUESTION_ID = Q.QUESTION_ID
AND A.USER_ID = Q.USER_ID
AND A.USER_ID = ID_IN
AND Q.USER_ID = ID_IN

SQL - Query Assistance [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm pretty new to SQL and I am working on an assignment that requires me to find what movies were created by a company with "films" in its name based off the IMDB database.
A diagram of this database can be viewed here:
http://i.imgur.com/kj8qVgF.png
This is the query I was working on.
SELECT t.title, t.id
FROM title t, movie_link m
JOIN movie_companies c ON (m.movie_id = c.movie_id)
JOIN company_name n USING (id)
WHERE n.name LIKE '%films%'
Your code does seem to be somewhat close to what you'll need, but there are some weird things there. First of all, the implicit join here title t, movie_link m is performing a cartesian product between those tables. This code should work:
SELECT DISTINCT t.id, t.title
FROM title t
INNER JOIN movie_companies mc
ON t.id = mc.movie_id
INNER JOIN company_name cn
ON mc.company_id = cn.id
WHERE cn.name LIKE '%films%'
There are several things that you need to take into account from that diagram to get the results that you want. For instance, since different companies can be the creator of the same movie (the diagram allows it), you'll need to use DISTINCT otherwise you might get duplicate results.
Seems like you are missing a join predicate for the join between title and movie_link tables.
I recommend you NOT mix the "comma" and the "JOIN keyword" syntax in a statement. That is, remove that comma between title and movie_link) and replace it with the keyword JOIN. And add a join predicate, if you're not wanting a cross join.
Also, the USING syntax in the join predicate is a little ambiguous for my taste. I recommend you explicitly specify which columns from which tables you want to match, like you did for the join to movie_companies table.
The query below isn't runnable. The ??? need to be replaced with appropriate identifiers and aliases:
SELECT t.title
, t.id
FROM title t
JOIN movie_link m ON m.??? = t.???
JOIN movie_companies c ON c.movie_id = m.movie_id
JOIN company_name n ON n.id = ???.???
WHERE n.name LIKE '%films%'