Linking tables in MySQL

Discussion in 'MySQL' started by David Reynolds, Oct 29, 2024.

  1. David Reynolds

    David Reynolds New Member

    Joined:
    Jul 20, 2024
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Hi,
    I've a table containing users. Being imaginative, I've called it users. There are two types of user, a driver and a client.
    A client rings and requests a driver to take them to a destination. The destinations are in a table called places. I have a table containing requests, which contains the appointment date/time, a client id, and a driver id. I need to list all outstanding requests, but bear in mind that some may not have a driver assigned yet, so the driver id may be 0.
    I tried this:
    Code:
    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;
    
    No syntax errors, but nothing was produced. I'm not familiar with JOIN, and this is probably partly due to my misunderstanding of how it works, so very grateful to be corrected.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice