SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Efficiently Access All Lookup Pairs

    (Bad title. Sorry.)

    Let's say I have some jokes, and I have some categories. Jokes can have multiple categories and categories can contain multiple jokes, so I create a lookup table.

    No problem. But then I decide that I want to display all my jokes on one page. I'll probably use some code similar to the following:

    PHP Code:
    $sql 'SELECT text FROM jokes';
    $jokes mysqli_query($link$sql);
    while (
    $joke mysqli_fetch_array($jokes)) {
        echo 
    $joke['text'].'<br />';

    This gives me a nice page with each joke on its own line. But what if I also wanted to display the categories a joke belongs under? This was the first solution I came up with, and it sucks for obvious reasons:

    PHP Code:
    $sql 'SELECT id, text FROM jokes';
    $jokes mysqli_query($link$sql);
    while (
    $joke mysqli_fetch_array($jokes)) {
        echo 
    $joke['text'];
        
    $sql 'SELECT category_id FROM lookup_table WHERE id = '.$joke['id'];
        
    $category_ids mysqli_query($link$sql);
        while (
    $category_id mysqli_fetch_array($category_ids)) {
            
    $sql 'SELECT name FROM categories WHERE id = '.$category_id['category_id'];
            
    $category mysqli_query($link$sql);
            
    $category mysqli_fetch_array($category);
            
    $category $category['name'];
            echo 
    $category.', ';
        }
        echo 
    '<br />';

    This may accomplish the task, but holy crap, I'm making multiple queries to the database for each individual joke (not to mention that, when I list the category names, it ends with a hanging comma).

    Please tell me there's a better way.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    JOIN!

    Code:
    SELECT
      jokes.text,
      categories.name
    FROM  jokes
    INNER JOIN lookup_table ON lookup_table.id = jokes.id
    INNER JOIN categories ON categories.id = lookup_table.category_id
    I highly recommend Simply SQL

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,247
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    thanks dan

    i like the idea of using GROUP_CONCAT on the categories, this way you get one row per joke and don't have to do all that fancy looping in the app...
    Code:
    SELECT jokes.text
         , GROUP_CONCAT(categories.name) AS categories
      FROM jokes
    INNER 
      JOIN lookup_table 
        ON lookup_table.id = jokes.id
    INNER 
      JOIN categories 
        ON categories.id = lookup_table.category_id
    GROUP
        BY jokes.text
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Oh man, GROUP_CONCAT is perfect. Thank you.


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
  •