How to make a query with the data of another query on SQL?












0














I have a user table and another order table. A user can have many orders. How do I get the last 1000 users and those users use my last 1000 orders for each one?



Query - Get users



select distinct users.id, users.first_name, users.last_name
from users
limit 2;


Query - Get orders



select distinct orders.id, orders.user_id
from orders
limit 2;









share|improve this question
























  • MySQL <> PostgreSQL. Please specify which RDBMS are you using ?
    – Madhur Bhaiya
    Nov 20 at 18:31










  • Sorry, I am using PostgreSQL
    – Arturo Verbel De León
    Nov 20 at 18:33






  • 2




    to know the "last" you will need to know when the users made the order. namely, add something like "purchase_date"
    – Sharon Ben Asher
    Nov 20 at 20:31
















0














I have a user table and another order table. A user can have many orders. How do I get the last 1000 users and those users use my last 1000 orders for each one?



Query - Get users



select distinct users.id, users.first_name, users.last_name
from users
limit 2;


Query - Get orders



select distinct orders.id, orders.user_id
from orders
limit 2;









share|improve this question
























  • MySQL <> PostgreSQL. Please specify which RDBMS are you using ?
    – Madhur Bhaiya
    Nov 20 at 18:31










  • Sorry, I am using PostgreSQL
    – Arturo Verbel De León
    Nov 20 at 18:33






  • 2




    to know the "last" you will need to know when the users made the order. namely, add something like "purchase_date"
    – Sharon Ben Asher
    Nov 20 at 20:31














0












0








0







I have a user table and another order table. A user can have many orders. How do I get the last 1000 users and those users use my last 1000 orders for each one?



Query - Get users



select distinct users.id, users.first_name, users.last_name
from users
limit 2;


Query - Get orders



select distinct orders.id, orders.user_id
from orders
limit 2;









share|improve this question















I have a user table and another order table. A user can have many orders. How do I get the last 1000 users and those users use my last 1000 orders for each one?



Query - Get users



select distinct users.id, users.first_name, users.last_name
from users
limit 2;


Query - Get orders



select distinct orders.id, orders.user_id
from orders
limit 2;






postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 18:34









Madhur Bhaiya

19.5k62236




19.5k62236










asked Nov 20 at 18:28









Arturo Verbel De León

305




305












  • MySQL <> PostgreSQL. Please specify which RDBMS are you using ?
    – Madhur Bhaiya
    Nov 20 at 18:31










  • Sorry, I am using PostgreSQL
    – Arturo Verbel De León
    Nov 20 at 18:33






  • 2




    to know the "last" you will need to know when the users made the order. namely, add something like "purchase_date"
    – Sharon Ben Asher
    Nov 20 at 20:31


















  • MySQL <> PostgreSQL. Please specify which RDBMS are you using ?
    – Madhur Bhaiya
    Nov 20 at 18:31










  • Sorry, I am using PostgreSQL
    – Arturo Verbel De León
    Nov 20 at 18:33






  • 2




    to know the "last" you will need to know when the users made the order. namely, add something like "purchase_date"
    – Sharon Ben Asher
    Nov 20 at 20:31
















MySQL <> PostgreSQL. Please specify which RDBMS are you using ?
– Madhur Bhaiya
Nov 20 at 18:31




MySQL <> PostgreSQL. Please specify which RDBMS are you using ?
– Madhur Bhaiya
Nov 20 at 18:31












Sorry, I am using PostgreSQL
– Arturo Verbel De León
Nov 20 at 18:33




Sorry, I am using PostgreSQL
– Arturo Verbel De León
Nov 20 at 18:33




2




2




to know the "last" you will need to know when the users made the order. namely, add something like "purchase_date"
– Sharon Ben Asher
Nov 20 at 20:31




to know the "last" you will need to know when the users made the order. namely, add something like "purchase_date"
– Sharon Ben Asher
Nov 20 at 20:31












1 Answer
1






active

oldest

votes


















1














First you need to know.. When it comes to query data don't do separate operation.. If you put out All Users and Orders Your data will be unordered and not consistency.. So you need to make Join to see All User with Order they have.. And as stated by @Sharon you need to add column date_ordered to see that order make.. I am assume you already have that column but i will call that column with date_ordered..



And your query will be :



select
users.id,
users.first_name,
users.last_name,
orders.id
from
users
inner join orders on users.id = orders.user_id
order by
orders.date_ordered desc
limit 1000


By order date_ordered use desc you will get the latest all the user with their order.. And i assume user_id column in table orders have constraint foreign key references to table users with column id..






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53399298%2fhow-to-make-a-query-with-the-data-of-another-query-on-sql%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









    1














    First you need to know.. When it comes to query data don't do separate operation.. If you put out All Users and Orders Your data will be unordered and not consistency.. So you need to make Join to see All User with Order they have.. And as stated by @Sharon you need to add column date_ordered to see that order make.. I am assume you already have that column but i will call that column with date_ordered..



    And your query will be :



    select
    users.id,
    users.first_name,
    users.last_name,
    orders.id
    from
    users
    inner join orders on users.id = orders.user_id
    order by
    orders.date_ordered desc
    limit 1000


    By order date_ordered use desc you will get the latest all the user with their order.. And i assume user_id column in table orders have constraint foreign key references to table users with column id..






    share|improve this answer




























      1














      First you need to know.. When it comes to query data don't do separate operation.. If you put out All Users and Orders Your data will be unordered and not consistency.. So you need to make Join to see All User with Order they have.. And as stated by @Sharon you need to add column date_ordered to see that order make.. I am assume you already have that column but i will call that column with date_ordered..



      And your query will be :



      select
      users.id,
      users.first_name,
      users.last_name,
      orders.id
      from
      users
      inner join orders on users.id = orders.user_id
      order by
      orders.date_ordered desc
      limit 1000


      By order date_ordered use desc you will get the latest all the user with their order.. And i assume user_id column in table orders have constraint foreign key references to table users with column id..






      share|improve this answer


























        1












        1








        1






        First you need to know.. When it comes to query data don't do separate operation.. If you put out All Users and Orders Your data will be unordered and not consistency.. So you need to make Join to see All User with Order they have.. And as stated by @Sharon you need to add column date_ordered to see that order make.. I am assume you already have that column but i will call that column with date_ordered..



        And your query will be :



        select
        users.id,
        users.first_name,
        users.last_name,
        orders.id
        from
        users
        inner join orders on users.id = orders.user_id
        order by
        orders.date_ordered desc
        limit 1000


        By order date_ordered use desc you will get the latest all the user with their order.. And i assume user_id column in table orders have constraint foreign key references to table users with column id..






        share|improve this answer














        First you need to know.. When it comes to query data don't do separate operation.. If you put out All Users and Orders Your data will be unordered and not consistency.. So you need to make Join to see All User with Order they have.. And as stated by @Sharon you need to add column date_ordered to see that order make.. I am assume you already have that column but i will call that column with date_ordered..



        And your query will be :



        select
        users.id,
        users.first_name,
        users.last_name,
        orders.id
        from
        users
        inner join orders on users.id = orders.user_id
        order by
        orders.date_ordered desc
        limit 1000


        By order date_ordered use desc you will get the latest all the user with their order.. And i assume user_id column in table orders have constraint foreign key references to table users with column id..







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 5:12

























        answered Nov 21 at 0:00









        dwir182

        1,368618




        1,368618






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2fstackoverflow.com%2fquestions%2f53399298%2fhow-to-make-a-query-with-the-data-of-another-query-on-sql%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin