我想知道是否有人可以告诉我如何使用L4的查询构建器编写以下查询。
SELECT u.user_id,c.c_id,u.username,u.email
FROM conversation c, users u
WHERE CASE
WHEN c.user_one = '$user_one'
THEN c.user_two = u.user_id
WHEN u.user_two = '$user_one'
THEN c.user_one= u.user_id
END
AND (
c.user_one ='$user_one'
OR c.user_two ='$user_one'
)
Order by c.c_id DESC
我似乎无法在L4 doc中找到关于CASE的任何内容
干杯,
答案 0 :(得分:1)
Laravel 4使用Query Builder
不支持此功能。但是,您可以使用常规select
函数
$params = array($user_one, $user_one, $user_one, $user_one);
$results = DB::select(
"SELECT u.user_id, c.c_id, u.username, u.email
FROM conversation c, users u
WHERE
CASE
WHEN c.user_one = ?
THEN c.user_two = u.user_id
WHEN u.user_two = ?
THEN c.user_one = u.user_id
END
AND (
c.user_one = ?
OR c.user_two = ?
)
ORDER BY c.c_id DESC",
$params
);
您可以在Laravels database documentation上阅读有关此内容的更多信息。