SQL Query with multiple JOINS and junction table












0















I'm working with MySQL on this query and struggling a bit. The joins make sense to me but getting the data out of the images table which has a junction table called user_image seems difficult and I just can't grasp it.



SELECT c.id, c.city, ROUND(( 3959 * acos( cos( radians(27.9861) ) *
cos( radians( lat ) ) *
cos( radians( lng ) - radians(-80.6628) ) +
sin( radians(27.9861) ) * sin(radians(lat)) ) ),0) AS distance,
t.id, t.name, t.price, t.duration, d.description, u.fname,
i.image_path, i.image_name
FROM city c
JOIN trip t ON c.id = t.city_id
JOIN trip_description d ON t.id = d.trip_id
JOIN user u ON t.user_id = u.id
-- Need to get all images that match trip and is_main = 1
HAVING distance < 20
ORDER BY distance
LIMIT 0 , 45;


My image tables look like this...





  • user_image: trip_id | image_id


  • image: id | image_name | is_active | is_main


Not sure if I'm supposed to be using another join, a union, a query in a query? Really at a loss, would appreciate some help :)










share|improve this question
















bumped to the homepage by Community 11 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • The geo portion of the query was from an example by Google, they used 'having' in their example so that's why it's there. Does that make a difference for trying to get data out of the image table?

    – Bryan
    Aug 2 '18 at 11:50
















0















I'm working with MySQL on this query and struggling a bit. The joins make sense to me but getting the data out of the images table which has a junction table called user_image seems difficult and I just can't grasp it.



SELECT c.id, c.city, ROUND(( 3959 * acos( cos( radians(27.9861) ) *
cos( radians( lat ) ) *
cos( radians( lng ) - radians(-80.6628) ) +
sin( radians(27.9861) ) * sin(radians(lat)) ) ),0) AS distance,
t.id, t.name, t.price, t.duration, d.description, u.fname,
i.image_path, i.image_name
FROM city c
JOIN trip t ON c.id = t.city_id
JOIN trip_description d ON t.id = d.trip_id
JOIN user u ON t.user_id = u.id
-- Need to get all images that match trip and is_main = 1
HAVING distance < 20
ORDER BY distance
LIMIT 0 , 45;


My image tables look like this...





  • user_image: trip_id | image_id


  • image: id | image_name | is_active | is_main


Not sure if I'm supposed to be using another join, a union, a query in a query? Really at a loss, would appreciate some help :)










share|improve this question
















bumped to the homepage by Community 11 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • The geo portion of the query was from an example by Google, they used 'having' in their example so that's why it's there. Does that make a difference for trying to get data out of the image table?

    – Bryan
    Aug 2 '18 at 11:50














0












0








0


1






I'm working with MySQL on this query and struggling a bit. The joins make sense to me but getting the data out of the images table which has a junction table called user_image seems difficult and I just can't grasp it.



SELECT c.id, c.city, ROUND(( 3959 * acos( cos( radians(27.9861) ) *
cos( radians( lat ) ) *
cos( radians( lng ) - radians(-80.6628) ) +
sin( radians(27.9861) ) * sin(radians(lat)) ) ),0) AS distance,
t.id, t.name, t.price, t.duration, d.description, u.fname,
i.image_path, i.image_name
FROM city c
JOIN trip t ON c.id = t.city_id
JOIN trip_description d ON t.id = d.trip_id
JOIN user u ON t.user_id = u.id
-- Need to get all images that match trip and is_main = 1
HAVING distance < 20
ORDER BY distance
LIMIT 0 , 45;


My image tables look like this...





  • user_image: trip_id | image_id


  • image: id | image_name | is_active | is_main


Not sure if I'm supposed to be using another join, a union, a query in a query? Really at a loss, would appreciate some help :)










share|improve this question
















I'm working with MySQL on this query and struggling a bit. The joins make sense to me but getting the data out of the images table which has a junction table called user_image seems difficult and I just can't grasp it.



SELECT c.id, c.city, ROUND(( 3959 * acos( cos( radians(27.9861) ) *
cos( radians( lat ) ) *
cos( radians( lng ) - radians(-80.6628) ) +
sin( radians(27.9861) ) * sin(radians(lat)) ) ),0) AS distance,
t.id, t.name, t.price, t.duration, d.description, u.fname,
i.image_path, i.image_name
FROM city c
JOIN trip t ON c.id = t.city_id
JOIN trip_description d ON t.id = d.trip_id
JOIN user u ON t.user_id = u.id
-- Need to get all images that match trip and is_main = 1
HAVING distance < 20
ORDER BY distance
LIMIT 0 , 45;


My image tables look like this...





  • user_image: trip_id | image_id


  • image: id | image_name | is_active | is_main


Not sure if I'm supposed to be using another join, a union, a query in a query? Really at a loss, would appreciate some help :)







mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 20 '18 at 2:18









Rick James

42.9k22259




42.9k22259










asked Aug 2 '18 at 11:13









BryanBryan

1




1





bumped to the homepage by Community 11 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 11 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • The geo portion of the query was from an example by Google, they used 'having' in their example so that's why it's there. Does that make a difference for trying to get data out of the image table?

    – Bryan
    Aug 2 '18 at 11:50



















  • The geo portion of the query was from an example by Google, they used 'having' in their example so that's why it's there. Does that make a difference for trying to get data out of the image table?

    – Bryan
    Aug 2 '18 at 11:50

















The geo portion of the query was from an example by Google, they used 'having' in their example so that's why it's there. Does that make a difference for trying to get data out of the image table?

– Bryan
Aug 2 '18 at 11:50





The geo portion of the query was from an example by Google, they used 'having' in their example so that's why it's there. Does that make a difference for trying to get data out of the image table?

– Bryan
Aug 2 '18 at 11:50










1 Answer
1






active

oldest

votes


















0














Ok I think I got it. Here's the working example. Thanks to everyone that responded.



SELECT c.id, c.city, ROUND(( 3959 * acos( cos( radians(27.9861) ) * cos( radians( lat ) ) 
* cos( radians( lng ) - radians(-80.6628) ) + sin( radians(27.9861) ) * sin(radians(lat)) ) ),0) AS distance, t.id, t.name, t.price, t.duration, d.description, u.fname, i.path, i.name
FROM city c
JOIN trip t ON c.id = t.city_id
JOIN trip_description d ON t.id = d.trip_id
JOIN user u ON t.user_id = u.id
LEFT OUTER JOIN user_image ui ON ui.trip_id = t.id
LEFT OUTER JOIN image i ON ui.image_id = i.id
AND i.main=1
HAVING distance < 20
ORDER BY distance
LIMIT 0 , 45;





share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f213888%2fsql-query-with-multiple-joins-and-junction-table%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Ok I think I got it. Here's the working example. Thanks to everyone that responded.



    SELECT c.id, c.city, ROUND(( 3959 * acos( cos( radians(27.9861) ) * cos( radians( lat ) ) 
    * cos( radians( lng ) - radians(-80.6628) ) + sin( radians(27.9861) ) * sin(radians(lat)) ) ),0) AS distance, t.id, t.name, t.price, t.duration, d.description, u.fname, i.path, i.name
    FROM city c
    JOIN trip t ON c.id = t.city_id
    JOIN trip_description d ON t.id = d.trip_id
    JOIN user u ON t.user_id = u.id
    LEFT OUTER JOIN user_image ui ON ui.trip_id = t.id
    LEFT OUTER JOIN image i ON ui.image_id = i.id
    AND i.main=1
    HAVING distance < 20
    ORDER BY distance
    LIMIT 0 , 45;





    share|improve this answer




























      0














      Ok I think I got it. Here's the working example. Thanks to everyone that responded.



      SELECT c.id, c.city, ROUND(( 3959 * acos( cos( radians(27.9861) ) * cos( radians( lat ) ) 
      * cos( radians( lng ) - radians(-80.6628) ) + sin( radians(27.9861) ) * sin(radians(lat)) ) ),0) AS distance, t.id, t.name, t.price, t.duration, d.description, u.fname, i.path, i.name
      FROM city c
      JOIN trip t ON c.id = t.city_id
      JOIN trip_description d ON t.id = d.trip_id
      JOIN user u ON t.user_id = u.id
      LEFT OUTER JOIN user_image ui ON ui.trip_id = t.id
      LEFT OUTER JOIN image i ON ui.image_id = i.id
      AND i.main=1
      HAVING distance < 20
      ORDER BY distance
      LIMIT 0 , 45;





      share|improve this answer


























        0












        0








        0







        Ok I think I got it. Here's the working example. Thanks to everyone that responded.



        SELECT c.id, c.city, ROUND(( 3959 * acos( cos( radians(27.9861) ) * cos( radians( lat ) ) 
        * cos( radians( lng ) - radians(-80.6628) ) + sin( radians(27.9861) ) * sin(radians(lat)) ) ),0) AS distance, t.id, t.name, t.price, t.duration, d.description, u.fname, i.path, i.name
        FROM city c
        JOIN trip t ON c.id = t.city_id
        JOIN trip_description d ON t.id = d.trip_id
        JOIN user u ON t.user_id = u.id
        LEFT OUTER JOIN user_image ui ON ui.trip_id = t.id
        LEFT OUTER JOIN image i ON ui.image_id = i.id
        AND i.main=1
        HAVING distance < 20
        ORDER BY distance
        LIMIT 0 , 45;





        share|improve this answer













        Ok I think I got it. Here's the working example. Thanks to everyone that responded.



        SELECT c.id, c.city, ROUND(( 3959 * acos( cos( radians(27.9861) ) * cos( radians( lat ) ) 
        * cos( radians( lng ) - radians(-80.6628) ) + sin( radians(27.9861) ) * sin(radians(lat)) ) ),0) AS distance, t.id, t.name, t.price, t.duration, d.description, u.fname, i.path, i.name
        FROM city c
        JOIN trip t ON c.id = t.city_id
        JOIN trip_description d ON t.id = d.trip_id
        JOIN user u ON t.user_id = u.id
        LEFT OUTER JOIN user_image ui ON ui.trip_id = t.id
        LEFT OUTER JOIN image i ON ui.image_id = i.id
        AND i.main=1
        HAVING distance < 20
        ORDER BY distance
        LIMIT 0 , 45;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 2 '18 at 13:51









        BryanBryan

        1




        1






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f213888%2fsql-query-with-multiple-joins-and-junction-table%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Liste der Baudenkmale in Friedland (Mecklenburg)

            Single-Malt-Whisky

            Czorneboh