我想将一些SQL语句转换为DQL有帮助吗?
我有用户实体
-------user--------
id
username
email
password
...
--------------------
和一个拥有2个指向用户的属性的朋友实体
------friend----
id
friend_one
friend_two
statu
--------------
所以我的SQL语句是
UPDATE friend
SET status="1"
WHERE
(friend_one="$user_id" OR friend_two="$user_id")
AND
(friend_one="$friend_id" OR friend_two="$friend_id")
SELECT 'friend_one','friend_two','status' FROM friend
WHERE
(friend_one="$user_id" OR friend_two="$user_id")
AND
(friend_one="$friend_id" OR friend_two="$friend_id")
SELECT F.statu, U.username, U.id
FROM user U, friend F
WHERE
CASE
WHEN F.friend_one = '$user_id'
THEN F.friend_two = U.user_id
WHEN F.friend_two= '$user_id'
THEN F.friend_one= U.user_id
END
AND
F.statu='1'
任何帮助请:/
最佳答案
很好,但到目前为止您是否实际尝试过任何方法?如果您在问题中包含当前代码,那就太好了。
我建议您使用以下类似的查询:
(免责声明:请注意它们是很原始的,未经测试)
/* For update */
$result = $db->executeUpdate('UPDATE friend AS f SET f.status = "1" WHERE (f.friend_one = ? OR f.friend_one = ?) AND (f.friend_one = ? OR f.friend_two = ?)',
array($user_id, $user_id, $friend_id, $friend_id),
array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
);
/* For first select */
$stmt = $db->executeQuery('SELECT f.friend_one, f.friend_two, f.status from friend AS f
WHERE (f.friend_one = ? OR f.friend_one = ?) AND (f.friend_one = ? OR f.friend_two = ?)',
array($user_id, $user_id, $friend_id, $friend_id),
array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
);
$result = $stmt->fetchAll();
/* For second select */
$stmt = $db->executeQuery('SELECT f.status, u.username, u.id from friend AS f
INNER JOIN user as u ON u.user_id =
CASE WHEN f.friend_one = ? THEN f.friend_two WHEN f.friend_two = ? THEN f.friend_one END
AND f.status = "1"',
array($user_id, $user_id),
array(\PDO::PARAM_INT, \PDO::PARAM_INT)
);
$result = $stmt->fetchAll();
关于mysql - 我想将SQL转换为DQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36708695/