删除双向友谊查询(mysql)

时间:2013-11-28 12:25:09

标签: php mysql sql

我的数据库中的友谊是双向的。检查屏幕截图:

enter image description here

因此用户ID 14和1是朋友。

我想要一个可以由任何一方发起并且删除双向街道完全删除这两行的查询。

我该怎么做呢?这并不简单。问asker = id。

6 个答案:

答案 0 :(得分:1)

使用ANDOR从更简单的条件创建条件,例如

(asker_user_id = 14 AND asked_user_id = 1) OR
(asked_user_id = 14 AND asker_user_id = 1)

答案 1 :(得分:0)

您可以结合2个IN条件来实现此目标。

DELETE FROM foo WHERE asker_user_id IN (1, 14) AND asked_user_id IN (1, 14)

http://sqlfiddle.com/#!2/d1866/1

答案 2 :(得分:0)

Selfjoin:

SELECT *
FROM friendship asker
LEFT JOIN friendship asked
ON asker.asked_user_id = asked.asker_user_id
WHERE asker.asker_user_id IS NOT NULL

我现在无法测试(或者只是懒得创建一个sqlfiddle), 但它是这样的。

我可能会以某种方式转换问题和提问者,但请不要仅仅因为这一点而拒绝投票。

这是你可以测试的方式,如果你有两个(而不是选择,使用删除, 检查正确的字段后)

答案 3 :(得分:0)

DELETE FROM friendship
WHERE (asker_user_id = 14 AND asked_user_id = 1)
   OR (asker_user_id = 1 AND asked_user_id = 14)

答案 4 :(得分:0)

更新(和测试)

即使您不知道1和14之间的关系,我的方法仍然有效。如果您正在寻找真正的“(提问者= 14并且询问= 1)或者(询问= 14和提问者= 1)”,这很容易当你在想“这不简单”

这是我测试的内容。我希望这有助于某人(包括OP)。感谢。

给定数据

CREATE TABLE tab
(
  id int,
  asker_user_id int,
  asked_user_id int
);

insert into tab value(1, 2, 1);
insert into tab value(2, 15, 1);
insert into tab value(3, 14, 1);
insert into tab value(4, 1, 14);
insert into tab value(5, 5, 1);
insert into tab value(6, 1, 5);
insert into tab value(7, 2, 5);

Q1 - 找到所有关系

SELECT t1.asker_user_id, t1.asked_user_id
FROM tab AS t1 JOIN tab AS t2
  WHERE t1.asked_user_id = t2.asker_user_id
    AND t2.asked_user_id = t1.asker_user_id
+---------------+---------------+
| asker_user_id | asked_user_id |
+---------------+---------------+
|             1 |            14 |
|            14 |             1 |
|             1 |             5 |
|             5 |             1 |
+---------------+---------------+

Q2 - 删除前,请检查关系的记录ID

SELECT DISTINCT A.id as record_id
FROM tab A JOIN 
(
    // insert Q1 here
) B ON (A.asker_user_id = B.asker_user_id AND A.asked_user_id = B.asked_user_id)
       OR (A.asked_user_id = B.asker_user_id AND A.asker_user_id = B.asked_user_id)
+-----------+
| record_id |
+-----------+
|         3 |
|         4 |
|         5 |
|         6 |
+-----------+

Q3删除所有友情记录

DELETE A
FROM tab A JOIN 
(
    // insert Q1 here
) B ON (A.asker_user_id = B.asker_user_id AND A.asked_user_id = B.asked_user_id)
       OR (A.asked_user_id = B.asker_user_id AND A.asker_user_id = B.asked_user_id)

Query OK, 4 rows affected (0.00 sec) <== records which has friendship are deleted.

原稿。答案

未经测试,但我猜它会同时显示13和14

SELECT *
FROM tab A JOIN 
(
    SELECT t1.asker_user_id, t2.asked_user_id
    FROM tab AS t1 JOIN tab AS t2
      WHERE t1.asked_user_id = t2.asker_user_id
        AND t2.asked_user_id = t1.asker_user_id
) B ON (A.asker_user_id = B.asker_user_id AND A.asked_user_id = B.asked_user_id)
  OR A.asked_user_id = B.asker_user_id AND A.asker_user_id = B.asked_user_id)

当上述查询有效时,请按以下方式删除:

DELETE FROM tab
WHERE id IN (
    SELECT A.id
    FROM tab A JOIN 
    (
        SELECT t1.asker_user_id, t2.asked_user_id
        FROM tab AS t1 JOIN tab AS t2
          WHERE t1.asked_user_id = t2.asker_user_id
            AND t2.asked_user_id = t1.asker_user_id
    ) B ON (A.asker_user_id = B.asker_user_id AND A.asked_user_id = B.asked_user_id)
      OR A.asked_user_id = B.asker_user_id AND A.asker_user_id = B.asked_user_id)
)

答案 5 :(得分:0)

$slq = "SELECT * from Table where `id` = '13' "; //depending on user
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
$asker = $result['asker_user_id']; //14 //
$asked = $result['asked_user_id']; //1
$id = $result['id'];

$del_sql = "DELETE from table WHERE asked_user_id = '.$asker.' AND asker_user_id = '.$asked.'";
$del_query = mysql_query($del_sql);
$del_first = "Delete from table Where `id` = $id";
$del_first_query = mysql_query($del_first);