Hi,
I'm not even sure whether this is possible. I've three tables, one containing a location ID, and a location name. The second table is a users table, which contains the kind of information you'd expect, i.e. name address etc. The third table is a transactions table containing rows describing transactions which can be described in the following way.
A client requests to be driven somewhere. A driver volunteers for this job. The table contains the date of the trip, plus a user id for the client a user id for the place/location, and a user id for the driver.
Drivers and clients are both in the users table.
Ive set up foreign keys in the transactions table for the place id, and for the user id.
I now need to select and display the information, i.e.
Client name, driver name, place name. I'm not completely ignorant on this subject, and have used MySQL in the past, but not in this way.
1.Is this possible, given that the users file contains both clients and drivers. If not, just break the news, and I'll need to find another way.
2.If it is possible, can anyone give guidance as to the correct syntax for such a MySQL statement. There are other columns I need, but keeping it simple, how do I get:
Client name, Place name, and driver name. That should get me started.
Thanks.
October 28, 2024 at 5:16 pm
Put the same table in the FROM clause twice using a JOIN. The key will be to give each instance of the table a different alias... one called "Driver" and one called "Client" with different criteria for both and use "2 part naming" of aliasname.columnname for each column reference.
I've never programmed in MySQL but it should be possible to do this in MySQL.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 28, 2024 at 6:38 pm
Thanks for this. It confirms my googling explroations, and I'm about to try it.
I wonder what will happen on those requests where a driver hasn't yet been allocated. They still need to show in this current repoert.
I'll give it a try.
Thanks.
October 28, 2024 at 8:50 pm
Thanks for this. It confirms my googling explroations, and I'm about to try it.
I wonder what will happen on those requests where a driver hasn't yet been allocated. They still need to show in this current repoert.
I'll give it a try.
Thanks.
You could do an "OUTER JOIN" (Either Left, Right, or Full depending) so that a Client without a Driver (or vice versa or both) will be reported as a problem.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 29, 2024 at 6:55 pm
Hmm, this is what I have, and whilst it doesn't crash with any syntax errors, it produces nothing. Currently, the requests table has no drivers, but I still need it to work, i.e. produce results, if necessary, with the driver name null.
Here is my code, and I apologize in advance for not formatting it correctly. I'm using a screen reader, and getting it in the correct format is causing problems.
SELECT requests.r_id AS request_id,
requests.r_placeid as requests_pid,
requests.r_aptdate,
client.u_id AS client_id,
client.u_fname AS client_fname,
client.u_sname AS client_sname,
client.u_addr1 AS client_addr1,
client.u_postcode AS client_postcode,
driver.u_id AS driver_id,
driver.u_fname AS driver_fname,
driver.u_sname AS driver_sname,
places.p_name AS p_name
FROM requests
JOIN users AS client ON requests.r_userid = client.u_id
JOIN places ON requests.r_placeid = places.p_id
JOIN users AS driver ON requests.r_driverid = driver.u_id;
Try this... notice the only change is a couple of left outer join's for the drivers table (the word OUTER is optional... I left it out). I not only did these on the join of the Drivers table but also on the Places table, just to cover all eventualities.
SELECT requests.r_id AS request_id,
requests.r_placeid as requests_pid,
requests.r_aptdate,
client.u_id AS client_id,
client.u_fname AS client_fname,
client.u_sname AS client_sname,
client.u_addr1 AS client_addr1,
client.u_postcode AS client_postcode,
driver.u_id AS driver_id,
driver.u_fname AS driver_fname,
driver.u_sname AS driver_sname,
places.p_name AS p_name
FROM requests
JOIN users AS client ON requests.r_userid = client.u_id
LEFT JOIN places ON requests.r_placeid = places.p_id
LEFT JOIN users AS driver ON requests.r_driverid = driver.u_id;
--Jeff Moden
Change is inevitable... Change for the better is not.
October 30, 2024 at 2:15 pm
Jeff, This works perfectly. The LEFT JOIN did the trick. I'm investigating this as part of my quest for knowledge. Thanks!
November 4, 2024 at 9:48 pm
Jeff, This works perfectly. The LEFT JOIN did the trick. I'm investigating this as part of my quest for knowledge. Thanks!
Thank you for the feedback!
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply