dongwenyou4298 2014-12-22 23:16
浏览 81
已采纳

检查mysql列中是否存在值

Is there a way to check if a value exists in a mysql column? I have table songs, and there are some columns, one of them is called 'agent_ip' where i will put a list/array of all user ip's that will visit the site. I need to check if current user ip is present in column 'agent_ip'. Here is some of my code:

public function voteSong($song_id, $case, $agent_ip) {           
    $query = $this->link->prepare("SELECT * FROM songs WHERE id = ? LIMIT 1"); 
    $query->bindValue(1, $song_id);
    $query->execute();
    $rowcount = $query->rowCount();        

    if ($rowcount != 0)
    {
        if (!in_array($agent_ip, $r['ip']))
        {
            if ($case === 'like')
            {
                while($r = $query->fetch())
                {
                    $vote = $r['votes'] + 1;
                }
            } 
            elseif ($case === 'dislike')
            {
                while ($r = $query->fetch())
                { 
                    if ($r['votes'] > 0)
                    {
                        $vote = $r['votes'] - 1;
                    }
                    else
                    {
                        $vote = 0;
                    }
                }
            }
            $query = $this->link->prepare("UPDATE songs SET datetime = ?, votes = ?, agent_ip = ? WHERE id = ?"); 
            $query->execute(array(date("Y-m-d H:i:s"), $vote, $agent_ip, $song_id));
        }
    }
}

The line if(!in_array($agent_ip, $r['ip'])) contains the wrong function which won't work, but i need an alternative for mysql. $r['ip'] variable is data from the 'agent_ip' column which look like this 127.0.0.1, 127.0.0.1, 127.0.0.1 (using 127.0.0.1 just for example, every 127.0.0.1 is a different ip)

  • 写回答

2条回答 默认 最新

  • dqp99585 2014-12-22 23:25
    关注

    If you're only checking against a single IP, why don't you just modify your query from:

    "SELECT * FROM songs WHERE id = ? LIMIT 1"
    

    To:

    "SELECT * FROM songs WHERE id = ? AND agent_ip = ? LIMIT 1"
    

    It seems a bit wasteful to query your whole result set when you are only querying against a specific IP and returning a single row.

    EDIT: Your current method would be extremely inefficient, you are passing a unique agent_ip each time you want to query a song to check if the IP exists, that would be fine, but you are creating a new DB connection every time from which you pull back all info which belongs to that song.

    Lets say we have 1 song, and 3IP's, currently the application would work like this:

    1) Call the method, passing IP_1
    2) Query the database getting all songs for ID1
    3) Check if IP_1 is in the result set and do process
    
    4) Call the method, passing IP_2
    5) Query the database getting all songs for ID1
    6) Check if IP_2 is in the result set and do process
    
    7) Call the method, passing IP_3
    8) Query the database getting all songs for ID1
    9) Check if IP_2 is in the result set and do process
    

    As you can see, there is a lot of repetition here which is going to hinder your apps performance as it scales, you would be so much better modifying your current function to accept a list of results for a song which is pre-queried only once and then recursively call a check function by passing that result array with your unique IP address.

    UPDATE You stated I understand that i need to have 2 tables(1 = songs; 2 = votes). But i cannot imagine how i will get songs from database, arranged by votes quantity.

    You should read SQL's JOIN documentation, the concept is simple - JOIN allows you to pull back a more detailed set of information based on what you want to query, in your example you may want to find out how many votes a specific song has.

    Your tables may look like:

    Songs 
    SONG_ID     Primary Key
    SONG_TITLE  
    SONG_DURATION
    SONG_TAGS
    
    Votes
    VOTE_ID     Primary Key
    SONG_ID     Foreign Key - (references the song_id table)
    VOTE_RES    Bool (either 0 for no, 1 for yes)
    AGENT_IP    Who sent the vote
    

    You could then find out how many people said they liked the song by performing a join:

    SELECT * FROM songs 
    JOIN votes 
    ON songs.song_id = votes.song_id 
    WHERE songs.song_id = 1 
    AND votes.vote_res = 1;
    

    This would return all the song with the id of 1 and all of its associated likes. Hope that helps a bit :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输