如何在不使用子选择查询的情况下优化此查询?因为被告知不使用限制0,0。也是为了分页。
select
e.logo, e.id, r.id, r.userid, u.username, u.fullname, e.push_notificaciones, e.nombre, r.reservationdate, u.userphoto, s.direccion,
r.comment, r.restcomment, r.status, r.phonenumber, u.email,
(select u.fullname from users u where parseID = r.approvalbyuserid) as approvalfullname,
( select u.userphoto from users u where parseID = r.approvalbyuserid ) as approvaluserphoto
from
users u,
reservations r,
empresas e,
sucursales s
where
r.userid = 'AEnStpQk4B'
and e.id = r.restid
and u.parseID = r.userid
and s.id = r.sucursalid
limit
0,
20
答案 0 :(得分:2)
您可以将其重写为
SELECT
e.logo, e.id, r.id, r.userid,
u.username, u.fullname, e.push_notificaciones,
e.nombre, r.reservationdate, u.userphoto, s.direccion,
r.comment, r.restcomment, r.status, r.phonenumber, u.email,
a.fullname as approvalfullname,
a.userphoto as approvaluserphoto
FROM reservations r
JOIN users u ON u.parseID = r.userid
JOIN users a ON a.parseID = r.approvalbyuserid
JOIN empresas e ON e.id = r.restid
JOIN sucursales s ON s.id = r.sucursalid
WHERE
r.userid = 'AEnStpQk4B'
LIMIT 0, 20
只要用户中的所有值都存在。如果没有,您可能需要LEFT JOIN
对于分页,LIMIT的第一个数字应为:
number_of_lines_per_page*(page_num -1)
每页20个项目,第3页:
LIMIT 20*(3-1), 20
但是,如果您希望获得没有任何限制的所有结果并在显示代码中处理,请跳过LIMIT
行