我有一个LIKE表和一个BOOK表以及我的user_id。
我想只用BOOK NAME和AUTHOR
从BOOK表中拉出来我的json会:
{book_id:1,bookname:sample,author:sean,user_id:111}
表
-----------BOOK TABLE------------
ID ---- BOOK AUTHOR ---- BOOK NAME
1
2
...................................
USERS TABLE
ID-------NAME
1
2
.............
LIKE TABLE---------------------------
ID-----BOOK ID-------LIKER USER ID---
1
2
.....................................
答案 0 :(得分:0)
要获取与喜欢相关的所有数据,您可以运行此sql命令。 结果集可以循环并生成JSON。 确保在查询中正确地给出了所有列名。
select
b.book_id,
b.bookname,
b.author,
u.user_id
from
`like` l
inner join book b on b.book_id = l.book_id
inner join users u on u.user_id = l.user_id
如果您需要过滤数据,只需在最后一次加入后添加where条件
where u.user_id = {your user id}
答案 1 :(得分:0)
$stmt = $mysqli->prepare("SELECT book.*, users.*, like.* FROM like INNER JOIN
users ON like.liker_user_id = users.id INNER JOIN
book ON like.book_id = book.id WHERE
users.id = ?
");
$stmt->bind_param( "d", $user_id);
$stmt->execute();
$stmt->bind_result($col1);
// then fetch and close the statement