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

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.

Related

How to find a list of entries where they have the same id and certain part numbers? [closed]

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 3 years ago.
Improve this question
I have a table which has three columns: id, layer and part_number.
The table looks like this:
I'd like to find the id that has part_numbers: 412789, 412801 & 412806. Can anyone help?
Thanks in advance!
You can group by record_id, and do an "IN" statement, to grab all ID's with those part numbers, and then fetch all the unique record_id's with any of those part numbers. In this case, it would be 1:
SELECT record_id
FROM parts_table
WHERE part_number IN (412789, 412789, 412806)
GROUP BY record_id
You can use distinct:
SELECT distinct record_id
FROM parts_table
WHERE part_number IN (412789, 412789, 412806)
I think what you're trying to return here is all the records from your table, where the record_id is shared across the 3 part_numbers.
It's a little messy, however, you should be able to do this with the following SQL:
with cte as
(select record_id,count(*) cnt
from parts_table
where part_number in (412789,412801,412806)
group by record_id having count(*) =3
)
select * from parts_table p
join cte c on c.record_id=p.record_id
where p.part_number in (412789,412801,412806)
I can't see the image of your table, but something like this should do the trick:
SELECT id
FROM (
SELECT id, part_number
FROM MyTable
WHERE part_number IN (142789, 412801, 412806) -- Restrict part numbers
GROUP BY id, part_number -- Get one row per id / part_number
) src
HAVING COUNT(*) = 3 -- Only return id's with three rows (one for each part_number)
GROUP BY id
Give it a try and let me know.
Update
To support variable part_numbers, you should parameterize the query using a stored procedure. Here are some links to get you started:
Stored Procedures - Intro
Stored Procedures - Parameters
Stored Procedures - More

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

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

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.

selecting multiple items form multiple tables with min and max [closed]

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 6 years ago.
Improve this question
I'm having a little trouble figuring out this SQL statement. I have three tables employee, job, and job type.
job type is reference table. It has the name of the job, how long that job takes, and how much is charged for that job.
job has records of what job was performed for what client by which employee and the date performed. It links to job type jobname.
employee has name of employee, and employee id. It links to job table by employee id.
I need to display a list of all employees as well as the cheapest and most expensive jobs they did for a given year.
I think I will probably have to have an embedded select and a few joins. Maybe a unionThe complexity is just too far out of my experience. I'm not even sure where to being to be honest.
You should use the employee table as the primary table in your query so you will get one row per employee, even if that employee is associated to 0 jobs. Then use outer joins to the job and job_type tables, group by the employee identifiers, and use the min() and max() aggregate functions on the job table to select the min and max job costs (if any).
Something like this should help you get started:
select employee.employee_id ,
employee.employee_name,
min(job_type.cost),
max(job_type.cost)
from employee
left outer join job on job.employee_id = employee.employee_id
left outer join job_type on job_type.job_type_id = job.job_type_id
group by employee.employee_id,
employee.employee_name

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)