Results 1 to 7 of 7

Thread: Sql query with join

  1. #1
    Join Date
    Jun 2009
    Posts
    3,859

    Sql query with join

    Here are the structure of my tables

    Comments
    --------------
    id auto_increment
    date
    Comment

    comment_member
    ------------------
    cm_id_comment
    cm_id_member

    User
    ---------------------
    id auto_increment
    username
    .....

    I wish I could retrieve the list of user reviews. I then test to make a request to retrieve my comments for user with id 1.

    Here are the data in the database.

    Comments
    --------------------

    1 2009-11-06 11:26:52 comments for user1
    2 2009-11-05 11:27:11 comments for user2

    comment_member
    ----------------------
    1 1
    2 2

    User
    -----------------
    1 user1
    2 user2

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Sql query with join

    But what you have tried? And by the way, your table comment_member has no interest, directly put the id of the member in the table comment.

  3. #3
    Join Date
    Jun 2009
    Posts
    3,859

    Re: Sql query with join

    If it has an interest because I have to differentiate the comments of a member or group.

    If I tried that
    Code:
    SELECT * FROM 'comment'
    INNER JOIN  'comment_member'
    ON comment_member.cm_id_member= 1
    But this returns me to the second user feedback too.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Sql query with join

    You did not do that with JOIN on the right field; the common field in both tables is the id of the comment. Your id_member = 1 will be in the WHERE clause.

  5. #5
    Join Date
    Jun 2009
    Posts
    3,859

    Re: Sql query with join

    Like this then?

    Code:
    SELECT *
    FROM 'comment'
    INNER JOIN 'comment_member' ON comment_member.cm_id_member = comment.id
    WHERE cm_id_member =1

  6. #6
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Sql query with join

    Didn't you thought on this for atleast 30 seconds! Why would your id_member be equal to the id of the comment?

  7. #7
    Join Date
    Jun 2009
    Posts
    3,859

    Re: Sql query with join

    Sorry for that but I am addicted to copy (CTRL + C) and paste (CTRL + V)

    Anyways then the code would be like this:
    Code:
    SELECT *
    FROM `comment`
    INNER JOIN `comment_member` ON comment_member.cm_id_comment = comment.id
    WHERE cm_id_member =1

Similar Threads

  1. Replies: 3
    Last Post: 02-06-2012, 12:44 PM
  2. Join two tables using SQL query
    By vaib288932 in forum Software Development
    Replies: 3
    Last Post: 20-08-2010, 12:01 PM
  3. How to Create view based on table-join query
    By Preetish in forum Windows Software
    Replies: 3
    Last Post: 12-08-2009, 01:44 PM
  4. Turn on MySQL query cache to speed up query performance
    By DMA2Superman in forum Software Development
    Replies: 3
    Last Post: 07-07-2009, 10:26 AM
  5. Equi Join vs Natural Join in SQL
    By Demetrius in forum Software Development
    Replies: 3
    Last Post: 14-05-2009, 12:55 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
  •  
Page generated in 1,714,169,401.74838 seconds with 16 queries