How to use join to link values from one table to another - mysql

I have two tables.
drivers
name number email
requests
id driverassigned ....
I want to get everything from drivers table that may or may not be mentioned in requests.driverassigned.
I have tried using join but it returns rows that have a match. Here is what I have so far.
select drivers.email
, drivers.`number`
, drivers.name
, requests.id
from drivers join requests on drivers.`number` like requests.driverassigned
I am sure there is a common solution but I don't have enough sql knowledge to fish it out.
Any Suggestions?

use left join
select drivers.email, drivers.`number`, drivers.name,
coalesce(requests.id,'none') as request_id
from drivers left join requests
on drivers.`number` like '%'+requests.driverassigned+'%'

select drivers.email, drivers.`number`, drivers.name, requests.id
from drivers left join requests on drivers.`number` = requests.driverassigned

Related

learning mysql, JOIN query

i'm a beginner on MYSQL db and i'm trying to play around with the query and relations.
i have created 2 tables, one is 'users' which contain the field staff_ID and the other is 'reports' which also contain the table field staff_ID of the user submitting the reports.
on the relations (see picture) i have connect the 2 staff id field.
every user can submit more than one reports, so i'm try to query and get only the reports of one users(staff_ID).
I understood i have to use the JOIN keyword in order to obtain the data..
i tried the following query but it gave me all the result for all the users.
SELECT u.staff_ID
, u.Name
, r.id_report_show
, r.date_report
FROM users u
JOIN reports r
ON r.staff_ID = u.staff_ID
but I would like to have the report only of one specific user like staff_ID = 04033
probably i understood wrong how this query JOIN work, i'm looking for some help.
Thanks
You are almost there. Your join is perfect. You just need a where clause.
SELECT users.staff_ID, users.Name, reports.id_report_show, reports.date_report
FROM `users` INNER JOIN reports ON reports.staff_ID = users.staff_ID
where users.staff_ID = 04033
Or you can also mention it within on clauses:
SELECT users.staff_ID, users.Name, reports.id_report_show, reports.date_report
FROM `users` INNER JOIN reports
ON reports.staff_ID = users.staff_ID and users.staff_ID = 04033
Since it's inner join both the query will produce same output. But for left join those might produce different result. It's a good practice to use where clause instead of mentioning the condition in on clause.

List of persons with fields from different tables

I'm a newbie with relational databases. My client asked for a list of clients with different fields by default. But in the schema are like 8 different tables and I need to make the list with fields for each one.
I was thinking of implementing a query like this one.
SELECT name, surname, country_of_birth, nationality, email
from clients,
client_documents as documents,
client_addresses as address
There are duplicate fields, but I don't know how to use the same uuid_client with it's the foreign key in each table.
you can use join and shorter table aliases so it will be easier for you to include its columns.
select t1.name
, t1.surname
, t1.country_of_birth
, t1.nationality
, t1.email
, t2.documents
, t3.city
from clients t1
inner join client_documents t2 on t1.uuid_client = t2.uuid_client
inner join client_addresses t3 on t1.uuid_client = t3.uuid_client
SELECT name, surname, country_of_birth, nationality, email
from clients, client_documents as documents, client_addresses as address
From the query above it seems you need to join the tables. But you didn't mention any of the fields from the remaining tables. So, if you have any common fields in these tables it is possible to use joins
Or Else you can try using UNION too.
Please Elaborate your question. So that I can answer it properly
One way to solve it is using JOIN.
For example :
SELECT clients.name, clients.surname, address.country_of_birth, clients.nationality, clients.email
FROM clients clients
LEFT JOIN client_documents AS documents on clients.id = documents.client_id
LEFT JOIN client_addresses AS address on clients.id = address.client_id
I suggest LEFT JOIN for this case, because you cannot see clients do not have address or documentation info if we use (INNER) JOIN. So we used LEFT join, but as I said the choice depends on your scenario.

mysql SELECT many to many GROUP_CONCAT (how to get all records)

this is my first question and sorry if my title is not very good and descriptive.
So i have this mysql query on many to many relationship and it is working fine, i get drivers and its units in format that i want.
select
d.*,
group_concat(DISTINCT u.make,'-', u.model)
from
drivers d
left join drivers_units dc
on d.driver_id = dc.driverId
left join units u
on dc.unitId = u.unit_id
My problem here is i don't get ALL drivers i get only drivers that have at least one unit i understand why (because that driver don't exist in driver_units table), my question here is:
What is best way to get all drivers (with and without any units), can i do it in one query (return all drivers no mater if they have units or not) or i must create new query to select drivers without units ?
I would appreciate mysql query example.
Something is wrong with your query, because you have an aggregation function (group_concat()) but no group by. Your query should be returning only one row.
To get all drivers, use the proper aggregation:
select d.*,
group_concat(distinct u.make,'-', u.model)
from drivers d left join
drivers_units dc
on d.driver_id = dc.driverId left join
units u
on dc.unitId = u.unit_id
group by d.driver_id;
I am guessing that the distinct is unnecessary.

Missing record in a complex SELECT FULL JOIN statement

I created a SQL statement that should return the number of appointments receive by all salesmen. I work with 3 tables, Contract, Salesmen and Appointment, and I need to show how many appointments was received by each salesmen.
My problem is that although I use a Full Join the result doesn't show people who didn't receive any appointments. I found that there is a problem about constraint.
I took a look to Except, Intercept and Union option but none of those could solve my problem. Which other way could I use to get the full list of reps having or not received some appointments?
There is an example of the statement I used:
SELECT C.RepID, COUNT(A.AppID) AS AppAttrib, C.AppointmentPurchased, S.Name, S.FirstName
FROM Repartition.dbo.Contract C
FULL JOIN Repartition.DBO.Appointment A
ON C.RepID = A.RepID
LEFT JOIN Repartition.DBO.Salesmen S
ON S.RepID = C.RepID
GROUP BY C.RepID, V.Nom, S.Name, S.FirstName
Thanks for your help,
Antenor
Not knowing your table structure in detail, I'm just guessing here - but I think your query starts at the wrong place - you should start with the Salesmen table, and go from there. So basically, select those columns from the Salesmen table that you need, and then join in the other tables as needed.
Something like this:
SELECT
s.RepID, S.Name, S.FirstName,
COUNT(A.AppID) AS AppAttrib,
C.AppointmentPurchased
FROM
Repartition.dbo.Salesmen s
LEFT OUTER JOIN
Repartition.dbo.Contract c ON s.RepID = c.RepID
LEFT OUTER JOIN
Repartition.dbo.Appointment a ON s.RepID = a.RepID
GROUP BY
s.RepID, s.Name, s.FirstName

Database design question - Performance needed

I created an inventory system which mostly is using a server-sided scripting language to do all the work. To try and get some performance gains I am looking to better design my database to try and minimizing the scripts.
I have a table named metal_part which has a one to one relationship with five other tables, basically the other tables are other parts, which those parts then have a one to one relationship with a few other tables.
When I query metal_part I need all the UPC numbers from each table, so its direct one to one relationships need to get their own information from their direct one to one relationship tables ect... Is it possible to make a huge query to build it all and put it in a form at like:
(###) - ####/##/##/## [a-z]
Using a query? or do I have to get all the information and concat it using a scripting language?
Thanks
You should be able to get all of the information you need using a standard join, and then, with the concat function appropriate to your database (see here http://www.1keydata.com/sql/sql-concatenate.html) you can form the string you want.
Your question is very vague.
I'm guessing you are talking about matching on a primary key called partnumber or something like that.
You can do this using a query like
SELECT mp.partnumber
, mp.UPC_number
, wp.UPC_number
, pp.UPC_number
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127';
You can also do
SELECT mp.partnumber
, group_concat(mp.UPC_number) as metal_UPCs
, group_concat(wp.UPC_number) as wood_UPCs
, group(concat(pp.UPC_number) as plastic_UPCs
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127'
GROUP BY mp.partnumber;
or
SELECT mp.partnumber
, concat_ws(','
, group_concat(mp.UPC_number)
, group_concat(wp.UPC_number)
, group(concat(pp.UPC_number)
) as UPCs_of_parts
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127'
GROUP BY mp.partnumber;