gpt4 book ai didi

mysql - pdo向sql结果添加值

转载 作者:行者123 更新时间:2023-11-29 21:15:43 25 4
gpt4 key购买 nike

我使用下面的代码从数据库获取数据

if( !empty( $books_ids ) )
{
$books_ids_in = implode(',', array_fill(0, count($books_ids), '?'));

$query = "SELECT
b.id,
b.`name`,
b.`year`,
GROUP_CONCAT(DISTINCT a.`name`) AS author_names,
GROUP_CONCAT(DISTINCT s.`name`) AS store_names
FROM
books AS b
LEFT JOIN books_authors AS b_a ON b.id = b_a.book_id
LEFT JOIN authors AS a ON a.id = b_a.author_id
LEFT JOIN books_stores AS b_s ON b.id = b_s.book_id
LEFT JOIN stores AS s ON s.id = b_s.store_id
WHERE
b.id IN (". $books_ids_in .")
GROUP BY b.id
ORDER BY b.id";

$stmt = $conn->prepare($query);
foreach ($books_ids as $k => $id) {
$stmt->bindValue(($k+1), $id);
}

$stmt->execute();
$results = $stmt->fetchAll();
}

当我使用 $book id 来实现此目的时,我想在结果中添加一些参数来表明,例如 param = "book"每行。有什么办法可以做到吗?

最佳答案

只需向其传递字符串并将其别名为列即可。不过,既然您知道代码中传入的值并通过代码显示这些值……我不确定您为什么需要这个……因为该值在显示时可以在代码中使用。

$query = "SELECT
b.id,
b.`name`,
b.`year`,
GROUP_CONCAT(DISTINCT a.`name`) AS author_names,
GROUP_CONCAT(DISTINCT s.`name`) AS store_names,
'". $param."' as forEveryRow
FROM
books AS b
LEFT JOIN books_authors AS b_a ON b.id = b_a.book_id
LEFT JOIN authors AS a ON a.id = b_a.author_id
LEFT JOIN books_stores AS b_s ON b.id = b_s.book_id
LEFT JOIN stores AS s ON s.id = b_s.store_id
WHERE
b.id IN (". $books_ids_in .")
GROUP BY b.id
ORDER BY b.id";

关于mysql - pdo向sql结果添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35960996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com