Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Posts
    217
    Thanks
    25
    Thanked 1 Time in 1 Post

    Question Need help with adding second select statement

    Hello, I hope someone here is willing to help me out with my sql problem.

    I've got three tables, each table is linked with each other by the field product_id.

    I want to select all unique product_id's from Table1 which have been generated between two certain dates (dates are in Table2).

    So far no problem, I wrote this sql statement:

    Code:
    SELECT DISTINCT product_id From Table1 
    INNER JOIN Table2 ON Table2.product_id=Table1.product_id 
    INNER JOIN Table3 ON Table3.product_id=Table1.product_id 
    where DATE(Table2.date_added)>='2014-02-01' and DATE(Table2.date_added)<='2014-03-01' Order by Table2.date_added DESC
    But I also need to retrieve a certain field ('Pr_orderid') from Table3, I haven't got a clue how to retrieve this field though since I allready have a select statement in my sql query.

    How can I build in an extra select statement for this Table3 field ?

  • #2
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    105
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Perhaps something like this?

    Code:
    SELECT DISTINCT(t1.product_id), t3.Pr_orderid 
    From Table1 t1 
    INNER JOIN Table2 t2 ON Table2.product_id = t1.product_id 
    INNER JOIN Table3 t3 ON Table3.product_id = t1.product_id 
    where DATE(t2.date_added)>='2014-02-01' and DATE(t2.date_added)<='2014-03-01' Order by t2.date_added DESC
    Assuming you didn't need any extra logic to display said field. Haven't really tested it however so give it a try and see what happens.

  • Users who have thanked Linux_Sage for this post:

    docock (03-24-2014)

  • #3
    Regular Coder
    Join Date
    Feb 2007
    Posts
    217
    Thanks
    25
    Thanked 1 Time in 1 Post
    That works like a charm, thank you very much!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •