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条)

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失