www.webdeveloper.com
Results 1 to 7 of 7

Thread: Problem with LEFT JOIN one to many

  1. #1
    Join Date
    Jan 2013
    Posts
    82

    Exclamation Problem with LEFT JOIN one to many

    Hi
    I've database with a table in which are memorized messages, and another table with votes of all messages

    For exemple
    messages: ID, text
    votes: userID, messageID, valutation

    Now i've to tak all messages along with respective votes, so this is my query

    SELECT messages.text, voted.valutation FROM messages LEFT JOIN votes ON messages.ID=votes.messageID

    The problem is that from the moment that each message can have more than ne vote, the result of query and of the fetching operations, retrieve me only one vote for each message!

    I don't know if the problem is in the query or in the fetch associative operation. Infact fetching as associative array, it assigns for each value a sector with an associative key taken from the column name... So, i think that, from the moment that i have "many" values associed with "valutation" name, it casue some overwites... right??

  2. #2
    Join Date
    Jul 2013
    Posts
    29
    Quote Originally Posted by American horizo View Post
    I don't know if the problem is in the query or in the fetch associative operation
    To clear this out you need to run this query either in console sql client or in some phpMyAdmin. If you do not have any of them, print each row right after fetching it from result set to check if the returned data looks like what you expect.

  3. #3
    Join Date
    Jul 2013
    Posts
    29
    I also suppose this is not direct copy-paste because there is a typo in your query and thus it will not work at all.

  4. #4
    Join Date
    Jan 2013
    Posts
    82
    Quote Originally Posted by tishetsky View Post
    I also suppose this is not direct copy-paste because there is a typo in your query and thus it will not work at all.
    in fact i've rearranged it for make it simpler, and yes, the typo is in the "voted" word
    Last edited by American horizo; 07-17-2013 at 02:40 AM.

  5. #5
    Join Date
    Jan 2013
    Posts
    82
    I've done as you said, launching the query from phpmyadmin and the results are ok... So at this point the problem is the fetching operation on duplicate associative keys. How can avoid that?

    EDIT: howewer i noted that the valutation of the same message are placed on different rows, while i need on the same
    Last edited by American horizo; 07-17-2013 at 03:00 AM.

  6. #6
    Join Date
    Jul 2013
    Posts
    29
    At this moment I would like you to show teh codez and the phpMyAdmin output for your query. In particlar I need to see how do you fetch data from the result set. The Manual says:
    PHP Code:
    $result mysql_query($sql);
    while (
    $row mysql_fetch_assoc($result)) {
        echo 
    $row["userid"];
        echo 
    $row["fullname"];
        echo 
    $row["userstatus"];
    }
    mysql_free_result($result); 
    In case you need the result to be processed further instead of immediate echo it would be
    PHP Code:
    while ($row mysql_fetch_assoc($result)) {
        
    $rows[] = $row;
    }
    mysql_free_result($result);
    print_r($rows); 
    It is pretty straightforward and I cannot imagine how it is possible to screw it up.

  7. #7
    Join Date
    Jun 2013
    Posts
    3
    Hi,
    SELECT messages.text, voted.valutation FROM messages LEFT JOIN votes ON messages.ID=votes.messageID
    try these in place of (voted) you can type (votes)

Thread Information

Users Browsing this Thread

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

Posting Permissions

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



Recent Articles