how to make count query and join two table [closed] - mysql

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.

Related

MySQL: Show all data from one query and join data from another query where it matches [closed]

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 2 years ago.
Improve this question
Trying to understand SQL more as part of a POC I'm working on and have hit a snag. I have two select queries (shown below) and I want to combine these into a single query that:
Shows all of the results of query one
Joins the result of query two if the category column matches.
Provides a default of 0 if there is no match
Query one:
SELECT activityId, location, category, activityScore FROM activities WHERE location = "manchester";
Query two:
SELECT userId, category, userScore FROM userscore s WHERE userId = "32976428";
Expected Output:
The resulting query should show all activities in "manchester" along with the associated userScore if the specified use has one that matches that category. If there is no userscore then 0 should be returned instead.
Thanks for any help.
Carl
I think you need a LEFT JOIN on your userscore table
SELECT a.activityId, a.location, a.category, a.activityScore,
s.userId, ISNULL(s.userScore,0) as userScore
FROM activities a
LEFT JOIN userscore s ON s.category = a.category AND s.userId = "32976428"
WHERE a.location = "manchester";

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

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

Accessing data from more than one table - SQL [closed]

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.

Select all rows from MySQL table with non-existent relation as empty cell [closed]

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)