douyan6548 2014-11-28 08:20
浏览 60
已采纳

如何编写这个复杂的MySQL查询

I have a table called Users and it has three columns Email , FB_Id , Google_ID

The question is how I can replicate following idea into just one MySQL query.

  1. Suppose someone login with FB_id then I should check in the DB if the email exist. If yes then update the related ID in this case FB_Id.

What I am trying to do is to keep all the id from different provider for a user. If a user has same user ID for google+ and Fb then we can have ID of both the provider for that email and my website will allow to login it in

The function that I have written is something Like this. Now I want to add the UPDATE action into this and then get the user details form the DB.

Function

function checkUpdate () {
    if ($this->email)
    {
       $query  =" SELECT * FROM `users` WHERE `Email` ='".$this->email."'";
    }
    else
    {
       echo "Email not exist ";
    }

    $rc = $this->parent->database->query($query);

    if (mysql_num_rows($rc)>0)
    {
        $this->flag_details = 'true';
    }

    while ($rfg=mysql_fetch_array($rc))
    {
        //print_r($rfg);

        $_POST['im_password'] = $rfg['Password'];
        $_POST['im_user'] = $rfg['Email'];
    }

}

Update1.

function checkUpdate () {
    if ($this->email)
    {
        $query  =" SELECT * FROM `users` WHERE `Email` ='".$this->email."'";
    }
    else
    {
        echo "Email not exist ";
    }

    $rc = $this->parent->database->query($query);
    if (mysql_num_rows($rc)>0)
    {
        $this->flag_details = 'true';

        //Here is my logic for it which I want to make in one sql query 
        if (isset($_POST['im_FbId']))
        {
            $sql = "UPDATE `users` SET fb_id='".$_POST['im_FbId']."' WHERE id=2";
        }

        if (isset($_POST['im_GId']))
        {
            $sql = "UPDATE `users` SET google_Id='".$_POST['im_GId']."' WHERE id=2";
        }

        $this->parent->database->query($sql);
    }

    while ($rfg=mysql_fetch_array($rc))
    {
        //print_r($rfg);

        $_POST['im_password'] = $rfg['Password'];
        $_POST['im_user'] = $rfg['Email'];
    }

}

Thanks in Advance

  • 写回答

2条回答 默认 最新

  • doulaobi7988 2014-11-28 12:46
    关注

    if i understand your question correctly you want to update both or one of google id and facebook id

    <?php
    
    $sql = "UPDATE `users` ";
    $sql .= isset($_POST['im_FbId']) ? " SET fb_id='{$_POST['im_FbId']}' " : "";
    $comma = isset($_POST['im_FbId']) ? "," : "";
    $sql .= isset($_POST['im_GId']) ? " {$comma}SET google_Id='{$_POST['im_GId']}' " : "";
    $sql .= " WHERE id=2";
    
    $this->parent->database->query($sql);
    

    if only one exists for example $_POST['im_FbId'] the $sql value will be

        UPDATE `users` SET fb_id='facebook id' WHERE id=2
    

    if both Id's exists the $sql value will be

        UPDATE `users` SET fb_id='facebook id' ,SET google_Id='google id' WHERE id=2
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度