+ Reply to Thread
Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By misson

Thread: MySQL Join Question

  1. #1
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    MySQL Join Question

    I'm trying to create a basket on an e-commerce site and am using sessions to store item id's in a temporary row in a "sessions" table.

    If an item from the "items" table is clicked, the MySQL insert (in the additem.php) adds the id ($_GET), separated by commas, to the "item_ids" column in table "sessions".

    In this way, the "item_ids" column in the sessions table can contain many id numbers - all comma separated.

    I'm now trying to finish off with the basket.php page which should return all items in that current session, together with all detail from table "items".

    So... (trying to get my head round joins)

    Something like..

    Code:
    SELECT items.*,sessions.* FROM items,sessions WHERE items.id LIKE %sessions.item_ids% AND sessions.session_id = $session_id
    But this returns a syntax error.

    I need to include a $session_id in here somewhere but I can't figure ..

    a) which join system I should be using and
    b) how I can filter where sessions.item_ids CONTAINS items.id
    b) how to limit the results by current $session_id

    I've read about JOIN types but can't really understand how to apply them in this situation.

    Help????

    Thanks

    Rich

    ---------- Post added at 07:43 PM ---------- Previous post was at 06:49 PM ----------

    OK - Sorry - worked it out.

    Code:
    SELECT items.*,sessions.*
    FROM items,sessions
    WHERE sessions.item_ids LIKE CONCAT('%',items.id,'%')
    AND sessions.session_id = '$session'
    Strange having to use CONCAT to get the wildcards in, but it works.

    Rich
    Last edited by learning_brain; 06-05-2011 at 02:44 PM.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: MySQL Join Question

    Columns in RDB tables should be simple, meaning a single value. This is the 1st normal form property. You should have a separate entry for each item in a cart. A practical reason for this is that joining using LIKE with a leading wildcard is incredibly inefficient. MySQL will need to scan the entire items table. If the table is in 1st normal form, MySQL can use the primary key index to find the rows directly.

    Code:
    -- you could also call this table `line_items`
    CREATE TABLE cart_items (
        `session` INT UNSIGNED,
        `item` INT UNSIGNED,
        CONSTRAINT `session_fk` FOREIGN KEY (`session`) REFERENCES `session` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
        CONSTRAINT `item_fk` FOREIGN KEY (`item`) REFERENCES `items` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    );
    
    -- the query:
    SELECT i.name, i.price, i.[...], s.id
      FROM items AS i
        JOIN cart_items AS ci ON i.id = ci.item
        JOIN sessions AS s ON ci.session = s.id
      WHERE s.id = :session
    For clarity's sake, join conditions (the sessions.item_ids LIKE CONCAT('%',items.id,'%') in your example) should be put in the JOIN clause rather than the WHERE clause.

    Don't use SELECT *; select only the columns you need.

    Quote Originally Posted by learning_brain View Post
    Strange having to use CONCAT to get the wildcards in, but it works.
    Not really. The arguments to LIKE must have string values. How else are you going to combine the pieces into a pattern (assuming PIPES_AS_CONCAT isn't set)?
    learning_brain likes this.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. MySQL question and a Mailing List question
    By CrazyPunch in forum Programming Help
    Replies: 4
    Last Post: 11-22-2010, 08:53 PM
  2. mysql join causing exception
    By the maya in forum Programming Help
    Replies: 0
    Last Post: 11-11-2009, 05:33 PM
  3. MySQL Join
    By jason32 in forum Programming Help
    Replies: 6
    Last Post: 08-01-2009, 08:26 PM
  4. Mysql+join+count
    By gptsven in forum Programming Help
    Replies: 9
    Last Post: 04-28-2009, 07:59 PM
  5. Join my forum and i will join your site
    By Daniel S in forum The Marketplace
    Replies: 4
    Last Post: 02-02-2008, 05:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers