Need Help on Select and Sub Query in MySQL..

Discussion in 'MySQL' started by toyotaMAN, Sep 27, 2006.

  1. #1
    ex..
    I have 2 tables pictures and comments

    table : pictures
    pid picture

    table : comments
    pid comments

    sample data on picture
    pid picture
    1 mypicture1.jpg
    2 mypicture2.jpg

    sample data on comments
    pid comments
    1 comments1
    1 comments2
    1 comments3
    2 comments1

    how to display the data in pictures and no. of comments..??

    sample
    pid picture comments
    1 mypicture1.jpg 3
    2 mypicture2.jpg 1



    I tried doing like this

    SELECT a.pid, a.picture,
    ( select count(*) from b where a.pid = b.pid) as comments
    FROM pictures a, comments b
    WHERE a.pid = b.pid

    But it doesnt work... Please need your help..Im using mysql 4.0

    Thanks very much
     
    toyotaMAN, Sep 27, 2006 IP
  2. 1-script.com

    1-script.com Well-Known Member

    Messages:
    805
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Pre-4.1 versions do not support nestled SELECT statemements, sorry. Ontop of that, what does your second select do anyways? You don't check that count(*) against anything. Plus, obviously, the ending bracket is missing. Did you omit a part of your original query? Anyways, see if you can upgrade to at least 4.1 if you absolutely need the nestled SELECTs.
     
    1-script.com, Sep 27, 2006 IP
  3. toyotaMAN

    toyotaMAN Peon

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In other website it works....I see that's why.....



    Get the total number of comments for each picture




    I cannot upgrade thats pre installed on the web host...

    Actually, I can do this with 2 queries.. but I would like to make it 1 for performance purposes...
     
    toyotaMAN, Sep 27, 2006 IP
  4. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #4
    Two queries would work fine, maybe even better/faster.
     
    SoKickIt, Sep 27, 2006 IP
  5. rosytoes

    rosytoes Peon

    Messages:
    230
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    SELECT a.pid, picture, count(
    COMMENTs )
    FROM pictures a , comments b
    WHERE a.pid = b.pid
    GROUP BY a.pid
    Code (markup):
     
    rosytoes, Sep 28, 2006 IP
    dct likes this.
  6. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #6
    
    SELECT 
        p.pid, p.picture, count(*)
    FROM pictures p 
        INNER JOIN comments c ON p.pid = c.pid
    GROUP BY 
        p.pid
    
    Code (sql):
    Similar to rosytoes, but explicitly stating the joins and in color :)
     
    dct, Sep 28, 2006 IP
  7. toyotaMAN

    toyotaMAN Peon

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Both of Them Worked!!!! yyyeeeeaaaaahhhhooooooo!!!!!

    Thanks rosytoes , dct !!! And to all who posted to this thread...

    Now, which one is faster?? hehehehehe.. ;)

    Again, many thanks!!!
     
    toyotaMAN, Sep 28, 2006 IP
  8. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #8

    You should add a new field to "pictures" table. You need "num_comments" or something like that so you don't have to count comments on every page load.
     
    SoKickIt, Sep 28, 2006 IP
  9. toyotaMAN

    toyotaMAN Peon

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    yes..actually im thinking about that... Im creating my own script for coppermine photo gallery to integrate into my forum.....I dont want to use other scripts(cpg_fetch)..its hard to customize...according to your needs..

    thanks sokickit :)
     
    toyotaMAN, Sep 28, 2006 IP