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
Related
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
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
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
i have two table alamat_penerima and lokasi
alamat penerima table
[this is my record
and then
lokasi table [this is the record
i wont to make two table like this
[view]
I hope you can help me
i have just try this query command :
SELECT COUNT(alamat_penerima.`nama_penerima`) AS penerima, lokasi.`wilayah`
FROM lokasi
INNER JOIN alamat_penerima ON alamat_penerima.`kota_kab` = lokasi.`wilayah`
GROUP BY alamat_penerima.`kota_kab`
but the result is
result
Your question is difficult to understand, so this answer is guesswork. It looks to me like you hope to list the number of recipient addresses in each region, but also show the regions with no addresses.
To do that, you need two subqueries joined together.
This subquery generates the names of all regions.
SELECT DISTINCT wilayah FROM lokasi
This subquery generates a list of regions with the count of addresses in each region. The result set from this subquery, however, omits regions with no addresses in them.
SELECT COUNT(*) num, kota_kab AS wilayah
FROM alamat_penerima
GROUP BY kota_kab
You can test these subqueries individually to determine whether they are correct. It's important to do that.
Finally, you join these two together as if they were tables. (That's why it's called structured query language).
SELECT w.wilayah, a.num
FROM (
SELECT DISTINCT wilayah FROM lokasi
) w
LEFT JOIN (
SELECT COUNT(*) num, kota_kab AS wilayah
FROM alamat_penerima
GROUP BY kota_kab
) a ON w.wilaya = a.wilaya
This will yield what you want, but showing NULL instead of 0 in rows with no addresses. (That's a result of using LEFT JOIN) You can put the 0 values there by using this as the first line of your query instead.
SELECT w.wilayah, IFNULL(a.num,0) num
The design trick is to make your first subquery determine the number of rows in your result set, then to use LEFT JOIN to put information from subsequent subqueries into your results.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Do tables have to be joined to extract data from them?
I believe you mean in one query. There are two ways I can think of to extract data for more than one table in a single query without "joining" them:
Fist is with union
SELECT A, B, C FROM Table1
UNION
SELECT X, Y, Z FROM Table2
You can also do a "cross join" which does not look like a join (and is probably what you are thinking of)
SELECT Table1.A, Table1.B, Table1.C, Table2.X, Table2,Y, Table2,Z
FROM Table1, Table2
As you can see from the syntax there is not relationship from one table to the other. This means that every row in table1 will be combined with every row in table2!
In my experience this is the most common mistake that programmers new to SQL make. They do a cross join when they mean a join and then they GROUP BY or DISTINCT to get the results they want. This is hugely inefficient!
Cross join can be good however, especially when you one of the table has just one row -- then you are adding those values to every row in other table. You are basically selecting single set of values for columns for every row.
Like if you want every row to have the maximum date (this is often done in reports)
SELECT *
FROM Table1, (SELECT MAX(updatedate) as Max_Update FROM Table1) AS MaxDate
Not necessarily, there are other options like Union:
SELECT customerNumber id, contactLastname name
FROM customers
UNION
SELECT employeeNumber id,firstname name
FROM employees
the above example is take from this.
There are other times you can run multiple queries, etc to get data from multiple sources with out a join. It all depends on what you want to do. However, join is a very common--and probably most usual--approach.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
If I had for example two tables called 'teachers' and 'lessons' and 'lessons' has a foreign key 'teacher_ID' referring to its counterpart in the 'teachers' table, then how would I select all rows from teacher with all of their corresponding lessons with an empty cell if no lesson is connected to them? I only was able to make MySQL show me the teachers that have one or more lessons attached.
Is this even possible without LEFT JOIN? I couldn't find anything on Google...
EDIT
I was interested in the mechanics of the LEFT JOIN keyword. But since there doesn't seem to be an alternative I'd say case closed.
The right way is using LEFT JOIN. This way if not match you will get (teacher_id), null
The LEFT JOIN keyword returns all the rows from the left table (teachers), even if there are no matches in the right table (lessons).
SELECT teacher.teacher_ID, lesson.lesson_ID
FROM teachers
LEFT JOIN lessons
ON teacher.teacher_ID = lesson.teacher_ID
If you want to emulate LEFT JOIN first use JOIN to find the element with match. And use UNION to add the rest with a value of NULL
SELECT teacher.teacher_ID, lessons.lesson_ID
FROM teachers
JOIN lessons
ON teacher.teacher_ID = lessons.teacher_ID
UNION
SELECT teacher.teacher_ID, null as lesson_ID
FROM teachers
WHERE NOT EXISTS ( SELECT 1
FROM lessons
WHERE lessons.teacher_id = teacher.teacher_id)