1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Best way to register votes?

Discussion in 'Databases' started by albatrosl, Oct 7, 2012.

  1. #1
    So in the website there are posts, and users can vote them. So there is an Id from the post and an Id from the user. Database is MySQL.

    I'm pretty shure what I'm doing is worng.

    I only have a varchar(22), and in it I put for exemple 233.788, that is: post id "." user id

    Otherways if I have two colums one for each id, it will have to search all the id from a post and then see if there is the other id of the user. I suppose this is the correct way perhaps faster seraching INT's, but using the other I just make and insert and if it fails that probably means the vote alredy exist because is unique key, and with only one simple query I do the job. With the correct methot I need a more complex query.

    I'm afraid I not explained it well so some exemples:

    What I'm donig:

    $vot_id = $post_id.".".$user_id;
    $sql = "INSERT INTO vot_post (vot_pos) VALUE ('$vot_id')";

    In the database: "varchar(22)" Unique Key. Exemple: 233.788


    What I think I should do but I really don't know If will be better or faster:

    $post_id = 233;
    $user_id = 788;
    $sql = "INSERT INTO vot_post(id_post,id_user) SELECT '$post_id', '$user_id' FROM DUAL WHERE NOT EXISTS (SELECT id_post FROM vot_post WHERE id_post='$post_id' AND id_user='$user_id' LIMIT 1)";
    In the database: "INT(10)" and "INT(12)"



     
    Last edited: Oct 7, 2012
    albatrosl, Oct 7, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    SELECT id_post FROM vot_post WHERE id_post = 233 AND id_user = 788;

    Then test for a row being returned. If one was, that user voted on that post already. (I'm assuming that the table is named vot_post.)

    The database will run just about as fast searching one field or two fields, it doesn't search first one, then the other.
     
    Rukbat, Oct 15, 2012 IP