SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question selecting columns based on queries from three tables

    table item has
    item_name
    .....

    table bill has
    bill_id
    qty
    .....

    table bill_id has
    date
    .....

    now my query is
    "select p.item_name,c.qty,c.bill_id
    from bill as c
    JOIN items as p ON p.id=c.id
    where p.category_name='$q'";
    i also want to add another condition in where clause so tat it also checks date form bill_id table

    how do i do it

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    What is the relationship between the bill_id table and the other two?

  3. #3
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bill will have a column bill_id which will have the same id(primary key) as tat of bill_id

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code MySQL:
    SELECT
        p.item_name
      , c.qty
      , c.bill_id
    FROM bill AS c
    INNER JOIN items AS p 
    ON p.id = c.id
    INNER JOIN bill_id AS bi
    ON bi.bill_id = c.bill_id
    WHERE p.category_name='$q'
    AND bi.billdate = ....
    Calling a column 'date' might result in errors, since it is a reserved word in MySQL. Unless you don't mind putting it in backticks each time you use it.

  5. #5
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks
    it worked gr8


Bookmarks

Posting Permissions

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