怎么能像mysql这样的格式查询?

时间:2016-07-21 13:58:17

标签: php mysql mysqli

我有一个包含大量这类记录的数据库。

tbl_friends in four entry。

###########################

id - user_id ------- friends ------

1 ------ 11 ------- -------------- 2,3,40,5,6,7,89,10  2 ------ 2 --------------------- 1,3,4,5,67,89,50

3 ---- - 3 --------------------- 1,2,54,6,7,8,78

4 ----- 40 ----------------------- 12,2,3,34,5,6,6,11

我希望运行查询列名'user_id '11删除'friends'列40,并且喜欢这种格式'2,3,5,6,7,89,10'和user_id 40删除数据库表中的11个视图这种格式
'12,2,3,34,5,6,6,11'如何替换查询实际上将在字符串内部工作,什么做它的模式匹配是不成功的,以提高任何性能 - 它实际上将生成工作查询喜欢。

像这样的图像视图查询 enter image description here

请帮帮我。 谢谢

2 个答案:

答案 0 :(得分:0)

问题是

如果用户删除了他的帐户,或者您决定删除他或者您更改了他的身份,则很难将他从每个与他成为朋友的人中删除

你应该使用外键并定义多对多的关系,这里有一些很好的答案。

System for keeping track of user favorites

答案 1 :(得分:0)

like this 

<?php
include('confi.php');
 if ($_SERVER['REQUEST_METHOD'] == "POST") {
 if (isset($_POST['user_id']) && isset($_POST['friend_id'])) {
    $user_id    = $_POST['user_id'];
    $friend_id  = $_POST['friend_id'];
    $sql1       = mysqli_query($conn, "SELECT * FROM tbl_friends WHERE user_id='$user_id'");
    $sql_chack  = mysqli_query($conn, "SELECT * FROM tbl_friend_request WHERE `user_id`='$user_id' AND `friend_id`='$friend_id' OR `user_id`='$friend_id' AND`friend_id`='$user_id'");
    $sql2       = mysqli_query($conn, "SELECT * FROM tbl_friends WHERE user_id='$friend_id'");
    $results1   = mysqli_fetch_array($sql1);
    $myString   = $results1['friends'];
    $results2   = mysqli_fetch_array($sql2);
    $myString2  = $results2['friends'];
    $myArray    = explode(',', $myString);
    $myArray2   = explode(',', $myString2);
    $del_val    = $friend_id;
    $del_val2   = $user_id;
    $to_remove  = explode(',', $del_val);
    $to_remove2 = explode(',', $del_val2);
    $result     = array_diff($myArray, $to_remove);
    $result2    = array_diff($myArray2, $to_remove2);
    $string     = implode(',', $result);
    $string2    = implode(',', $result2);
    if (mysqli_num_rows($sql_chack)) {
        /* echo $string.'<br>';
        echo $string2.'<br>'; */
        $sql_friend_user    = mysqli_query($conn, "UPDATE tbl_friends SET friends='$string' WHERE user_id='$user_id'");
        $sql_friends_friend = mysqli_query($conn, "UPDATE tbl_friends SET friends='$string2' WHERE user_id='$friend_id'");
        $sql_recent         = mysqli_query($conn, "DELETE FROM `tbl_recent` WHERE `user_id`='$user_id' AND `friend_id`='$friend_id' OR `user_id`='$friend_id' AND`friend_id`='$user_id'");
        $sql_friend         = mysqli_query($conn, "DELETE FROM `tbl_friend_request`  WHERE `user_id`='$user_id' AND `friend_id`='$friend_id' OR `user_id`='$friend_id' AND`friend_id`='$user_id'");
        $json               = array(
            "status" => 1,
            "msg" => "success"
        );
    } else {
        $json = array(
            "status" => 0
        );
    }

} else {
    $json = array(
        "status" => 0000
    );
}

 } else {
$json = array(
    "status" => 0,
    "msg" => "Request Method not accepted !"
);
 }
     mysqli_close($conn);
   header('Content-type: application/json');
      echo json_encode($json);
     ?>