MySQL匹配和不匹配组合

时间:2018-08-18 11:00:56

标签: php mysql sql licensing

我正在尝试创建条件声明以进行许可证验证。我有3个参数@model IEnumerable<MyViewModel> <div class="list-group"> @foreach (var x in Model) { <a href="#" class="list-group-item"> @int passPercentage = x.PassCount/(x.FailedCount + x.Passcount)*100; <span>passPercentage</span> </a> } </div> purchase codeitem id。我想检查它们是否在一起或部分匹配数据库行中的数据,如果不存在则插入。因此,我必须创建一个或多个SQL查询来执行此操作,但是我无法弄清楚什么是最干净/有效的方法。查看代码本身,您将了解我正在尝试做的事情。

website URL

1 个答案:

答案 0 :(得分:0)

也许这样会有所帮助:

$valid_license_sql = "SELECT 
    CASE WHEN Code='".$purchasecode."' THEN 1 ELSE 0 END as purchasecode,
    CASE WHEN ItemID='".$item_id ."' THEN 1 ELSE 0 END as itemid,
    CASE WHEN Website='".$website."' THEN 1 ELSE 0 END as website,
    CASE WHEN Code='".$purchasecode."' AND ItemID='".$item_id ."' AND Website='".$website."' THEN 1 ELSE 0 END as license
 FROM PurchaseCodes";

 $query = $mysqli->query($valid_license_sql);
 $proper= $query->fetch_assoc();



 if ( $proper['license'] ) {
             echo 'License is already active!';
         } elseif ( $proper['purchasecode'] && $proper['itemid'] && !proper['website']) {
             echo 'License is already actived on another website!';
         } elseif ( !$proper['itemid'] ) {
             echo 'This purchase code is for our another item';
         }else {
             // insert into database if not already exists
             $mysqli->query("INSERT IGNORE INTO PurchaseCodes (Code, ItemID, Website) VALUES ('$purchasecode', '$item_id', '$website')");
             echo 'License is successfully activated!';
         }

如果表中有更多行,只需遍历结果集即可。