duan01203 2017-01-08 23:19
浏览 113

匹配多个列值以获取记录匹配百分比

I have a table of records which contains first name, last name, mobile number, email of persons details. I want to check each time when I get these values passed from front end page to check whether first name matches to a record in Database with the same first name and also for last name and so on.. So if all four columns match to the parameters getting from front page some how then the matching percentage is 100%. Likewise if three parameters match then it'll be 75% and so on...

attempt...

    $firstName = 'peter';
    $lastName = 'cetera';
    $phoneNumber = '0384849494';
    $emailAddress = 'peter@abc.com';
    $matchPercentage;
    $stmt = $mysqli->prepare("SELECT 
                              person.firstName,
                              person.lastName,
                              person.mobileNo,
                              person.email
                            FROM
                              person
                              LEFT OUTER JOIN reg_person ON (person.personId = reg_person.personId)
                              AND (person.messageid = reg_person.messageId)
                              AND (person.firstName = reg_person.firstName)
                              AND (person.lastName = reg_person.lastName)
                              AND (person.mobileNo = reg_person.mobile)
                              AND (person.email = reg_person.email)
                            WHERE
                              person.firstName = ? AND 
                              person.lastName = ? AND 
                              person.mobileNo = ? AND 
                              person.email = ?");

    $stmt->bind_param("ssss",$firstName,$lastName,$phoneNumber,$emailAddress) or die($mysqli->error);
    $stmt->execute();
    $stmt->bind_result($firstName,$lastName,$mobileNo,$email);
    $stmt->store_result();
    $num_of_rows = $stmt->num_rows;
    if($num_of_rows > 0){
        $matchPercentage = '100%';
    }else{......

will it take 16 mysql queries to do it?... or is there a simpler way?

  • 写回答

1条回答 默认 最新

  • dongque6377 2017-01-08 23:30
    关注

    You can get the best matching record by counting the number of matching fields and then doing a reverse order by:

    SELECT p.*,
           ( (p.firstName = ?) + (p.lastName = ?) + (p.mobileNo = ?) + (p.email = ?) ) numMatches
    FROM person p LEFT OUTER JOIN
         reg_person rp
         ON p.personId = rp.personId AND
            p.messageid = rp.messageId AND
            p.firstName = rp.firstName AND
            p.lastName = rp.lastName AND
            p.mobileNo = rp.mobile AND
            p.email = rp.email
     WHERE p.firstName = ? OR 
           p.lastName = ? OR 
           p.mobileNo = ? OR 
           p.email = ?
    ORDER BY numMatches DESC
    LIMIT 1;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)