对于包含3个表的mysql查询,我有一个问题。我必须打印用户的id (在users表上),在book_request表中显示请求的时间,并在其他表上对书进行求和。
select
U.id AS ID,
B.time_request AS Time,
(select SUM(volume) from book_buyed O where O.user_id = 62) as BookBuyed
from book_request B
inner join users U on B.id_cliente = U.id
通过这段代码,它可以工作,但我必须手动选择id
如果我使用这段代码
select
U.id AS ID,
B.time_request AS Time,
(select SUM(volume) from book_buyed O where O.user_id = U.id) as BookBuyed
from book_request B
inner join users U on B.id_cliente = U.id
如果我使用此代码,BookBuyed列的结果每次都为null。
发布于 2019-06-29 18:21:25
我一直试图避免将select查询嵌入到另一个查询的选择列表中:
select
U.id AS ID,
B.time_request AS Time,
Bookbuyed.sumvol
from
book_request B
inner join users U on B.id_cliente = U.id
inner join (select user_id, SUM(volume) sumvol from book_buyed group by user_id) as BookBuyed on u.id = bookbuyed.user_id
在联接区域中这样做可以帮助我更好地将它们分割为“与其他数据块相连接的数据块”;在这里,您可以设想先计算和卷,然后将来自这个子查询的结果数据输入到其他连接中,就像它是一个表一样。
https://stackoverflow.com/questions/56819687
复制相似问题