Search 2 tables with 1 Query - Tables share a field

Discussion in 'PHP' started by rts5678, Nov 30, 2007.

  1. #1
    I have 2 tables. Both tables contain information about a customer order.

    Table 1 is called: orders
    Table 2 is called: order_details

    Both tables have a common field called 'orders_id'.

    By using a keyword (variable) I can query the field 'description' in Table 2 and output certain information from that table. No problem.

    However, one piece of information 'order_status' is missing in Table 2 and is only contained in Table 1.

    How do I write a query that will output records from Table 2, but for each outputted record (or customer order) I am also able to output the 'order_status' of that order which is contained in Table 1.

    I must mention again, that field 'orders_id' is common in both tables.

    I will appreciate help from the pros in here.
    Thanks
     
    rts5678, Nov 30, 2007 IP
  2. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try something like this:

    SELECT d.*, o.order_status
    FROM order_details d, orders o
    WHERE d.description LIKE '%<your variable>%' AND
    d.order_id = 0.order_id;

    You might not want to use LIKE but replace this with whatever you are using to search the description now.

    Hope this helps
     
    tonybogs, Nov 30, 2007 IP
  3. rts5678

    rts5678 Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Tony. With minor edits it worked.
     
    rts5678, Dec 3, 2007 IP