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
Related
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
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 7 years ago.
Improve this question
My query:
SELECT DISTINCT img.prd_id,prd.*,img.* FROM products_prd prd OUTER JOIN
prd_images_img img ON prd.prd_id=img.prd_id ORDER BY prd.prd_datetime
the primary key in products_prd is returned NULL. They have values. while img.prd_id may be NULL or not.
The problem is that you have name collisions -- two things called prd_id for example. You need to use aliases to rename the columns:
SELECT prd.*, img.col1 as img_col1, img.col2 as img_col2
FROM products_prd prd LEFT OUTER JOIN
prd_images_img img
ON prd.prd_id = img.prd_id
ORDER BY prd.prd_datetime;
You don't need to select prd_id twice.
Or, you can use USING instead of ON:
SELECT *
FROM products_prd prd LEFT OUTER JOIN
prd_images_img img
USING (prd_id)
ORDER BY prd.prd_datetime;
This returns the columns in the USING clause only once (although you could have problems with other columns if they have the same names in the two tables).
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)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 3 tables (match, player and goal),
and I need to display all matches, but only with the name of the last player who scored.
I know that I need to join my tables, but I don't know how.
here is my database diagram:
database diagram http://img843.imageshack.us/img843/582/8gqj.jpg
Or you can do
SELECT m.id, m.date, p.name, g.goal FROM match m
LEFT JOIN ( SELECT max(id) gid,id_match FROM goal GROUP BY id_match )
lastgoal ON lastgoal.id_match=m.id
LEFT JOIN goal g ON g.id=lastgoal.gid
LEFT JOIN player p ON p.id=g.id_player
The first subquery (with a GROUP BY) finds the last goal of each game on the assumption that it has the highest id (entered last), the rest is straight forward joins ...
Use LIMIT mixed with ORDER BY to select only one item from a table, then make your join.