Query help

Discussion in 'Databases' started by maiq, Feb 2, 2008.

  1. #1
    Hello, bare with me on this one

    3 tables
    users - id(int) username(varchar)
    articles - id(int) text(text)
    read - id(int) uid(int) aid(int) read(varchar default false)

    uid=id of the user that marked the article as read
    aid=marked article

    So users see a list of articles and have the option to mark it as read. Once they do this it should not show on the article list page.

    I`m currently selecting all from articles and for each article I check to see if the current logged user marked it as read or not. Problem is I have 1 query that selects the articles and for each row returned another query that checks against the read table.

    Any suggestions? There has to be a easier way but I just can`t find it..
     
    maiq, Feb 2, 2008 IP
  2. mnn888

    mnn888 Member

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    why not just left join it:
    select * from user u left join read r on r.uid=u.id left join articles a on a.id=r.aid
    where u.id= 'userid'

    then the column 'read' will indicate if the article has been read or not
     
    mnn888, Feb 2, 2008 IP
  3. maiq

    maiq Peon

    Messages:
    389
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    But if i have 2 users 10 articles and no reads, wouldn`t that return 0 rows?

    I want to select the articles that are not read by the current user, not the ones that are.
     
    maiq, Feb 3, 2008 IP
  4. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #4
    SELECT * FROM `articles`
    LEFT JOIN `read` ON `read`.`aid` = `articles`.`id` AND `read`.`uid` = [i]insert user_id here[/i]
    WHERE `read`.`read` IS NULL OR `read`.`read` = 'false'
    Code (markup):
     
    SoKickIt, Feb 3, 2008 IP
    maiq likes this.
  5. maiq

    maiq Peon

    Messages:
    389
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks, that worked great :D
     
    maiq, Feb 3, 2008 IP