Closed Thread
Results 1 to 5 of 5

Thread: SQL joins

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    SQL joins

    I'm making a web page that is supposed to display for any order what products have been ordered, how many products ordered, and what side the user has chosen.

    This data is located across several tables. I have a table products_ordered which serves as a link to the products table, so I can get the name of the product. The products_ordered table also links to the the sides table, so I can find out what the side is.

    The tables look like this:

    products table:
    Code:
    CREATE TABLE IF NOT EXISTS `products` (
      `prd_id` int(11) NOT NULL AUTO_INCREMENT,
      `prd_name` varchar(100) NOT NULL,
      `prd_active` enum('1','0') NOT NULL DEFAULT '1',
      PRIMARY KEY (`prd_id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=25 ;
    Products ordered:

    Code:
    CREATE TABLE IF NOT EXISTS `products_ordered` (
      `po_ord_id` int(11) NOT NULL COMMENT 'a foreign key that links to the order table.',
      `po_prd_id` int(11) NOT NULL COMMENT 'the product id this links to the products table.',
      `po_qty` tinyint(4) NOT NULL COMMENT 'the number of entrees the user has selected.',
      `po_side` int(11) NOT NULL COMMENT 'A link to the side that the user has requested.'
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    Order table:

    Code:
    CREATE TABLE IF NOT EXISTS `orders` (
      `ord_id` int(11) NOT NULL AUTO_INCREMENT,
      `ord_date` datetime NOT NULL,
      `ord_size` varchar(15) NOT NULL COMMENT 'Size of the order, full, split or single.',
      `ord_qty` tinyint(4) NOT NULL COMMENT 'number of entrees the user wants. depends on size',
      `ord_custid` varchar(16) NOT NULL COMMENT 'foreign key linking to the customers table.',
      `ord_refid` varchar(16) NOT NULL COMMENT 'foreign key linking to the referrals and customer tables.',
      `ord_grpid` int(11) NOT NULL,
      `ord_delivery` enum('1','0') NOT NULL COMMENT '1 means the user wants delivery. 0 means no delivery!',
      `ord_credit` double NOT NULL COMMENT 'The amount of credit used with this order.',
      PRIMARY KEY (`ord_id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=116 ;
    Sides table:

    Code:
    CREATE TABLE IF NOT EXISTS `sides` (
      `side_id` int(11) NOT NULL AUTO_INCREMENT,
      `side_name` varchar(50) NOT NULL,
      `side_active` enum('1','0') NOT NULL DEFAULT '1' COMMENT '1 means this side is being offered. 0 means that this side is no longer avaiable. display ''none'' for side.',
      PRIMARY KEY (`side_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;
    Basically the Orders table links to the products_ordered table, so I can find out what products were ordered. The products_ordered table links to the sides table and the products table. I'm not sure how I can get all the information I need from the database though.

    I know it uses joins, but SQL joins are really confusing me.

    I tried this:

    Code:
    $sql = "SELECT p.prd_name, po.po_qty, s.side_name FROM products as p, products_ordered AS po, sides AS s INNER JOIN products ON po.po_ord_id=orders.ord_id;";
    But it didn't work. Can someone help clear up my confusion?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: SQL joins

    Try taking the ; out from between the ;

    If your joining 3 tables - which I have only done once - I remember I did it one query where I joined the 2 tables, then put those in ()'s then I did another join - I actually used left join

    Code:
    $sql = "SELECT p.prd_name, po.po_qty, s.side_name FROM products as p, products_ordered AS po, sides AS s INNER JOIN products ON po.po_ord_id=orders.ord_id";
    It looks like your not joining P based on anything either

    SQL LEFT JOIN Keyword

    Id try and help more but im not very good with mysql and I dont want to install a bunch of tables to play with lol but here is a rough shot:

    Code:
    SELECT *
    FROM (products
    LEFT JOIN products_ordered
    ON products
    .po_prd_id=products.prd_id)
    LEFT JOIN orders
    ON products
    .po_ord_id=orders.ord_id 

  4. #3
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: SQL joins

    Thanks man, you are very helpful. I'm almost done, I'm just not sure how I can join a sides table to this:

    Code:
    $sql = "SELECT prd_name, po_qty FROM (orders INNER JOIN products_ordered ON orders.ord_id=products_ordered.po_ord_id) INNER JOIN products ON products.prd_id=products_ordered.po_prd_id WHERE ord_id='$id';";
    I want to join it on products_ordered.po_side=sides.side_id but I'm not sure where to add the next INNER join statement.

  5. #4
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: SQL joins

    Id assume:
    Code:
    $sql = "SELECT prd_name, po_qty FROM ((orders INNER JOIN products_ordered ON orders.ord_id=products_ordered.po_ord_id) INNER JOIN products ON products.prd_id=products_ordered.po_prd_id) --INNER JOIN HERE WHERE ord_id='$id';";

  6. #5
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: SQL joins

    Thanks

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. A new moderator joins the team
    By John in forum Announcements
    Replies: 16
    Last Post: 10-25-2010, 09:28 PM
  2. Using Set Theory to Understand SQL Joins
    By WingedPanther in forum Tutorials
    Replies: 21
    Last Post: 09-23-2010, 01:42 PM
  3. Joins vs. Separate Queries
    By BASHERS33 in forum Database & Database Programming
    Replies: 18
    Last Post: 05-07-2009, 08:32 AM
  4. Joins
    By NeedHelp in forum Database & Database Programming
    Replies: 2
    Last Post: 08-02-2006, 03:35 PM

Tags for this Thread

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