gpt4 book ai didi

php/mysql - 使用联合,如何根据选定的表设置变量?

转载 作者:太空宇宙 更新时间:2023-11-03 12:30:44 25 4
gpt4 key购买 nike

使用下面的代码,我显示状态和评论,它按预期工作,虽然我不完全理解我自己的代码..

我想实现以下目标:如果结果来自状态,$link_type = status,如果结果来自评论,$link_type = comment

这是我的尝试($link_type 现在总是'status')

    $results = db_query("
SELECT sid as ssid, created as screated, message as smessage, recipient as srecipient
FROM {statuses}
WHERE created > :logout_stamp
UNION
SELECT sid as ssid, created as screated, comment as smessage, uid as srecipient
FROM {fbss_comments} fbss
WHERE fbss.created > :logout_stamp
ORDER BY screated DESC LIMIT 15",
array(':logout_stamp' => $logout_stamp))->fetchAll();

// foreach the results
foreach ($results as $result) {
$user_status = user_load($result->srecipient);
$user_comment = user_load($result->commentuid);
$username_status = $user_status->name;
$username_comment = $user_comment->name;
$date_status = $result->screated;
$date_comment = $result->commentcreated;

if ($result->ccreated != NULL) {
$link_type = "comment";
}
else {
$link_type = "status";
}

print '<a href="statuses/' . ($result->ssid) . '" class="notification_wrapper">' . '<b>' . $username_status . '</b>' . ' - ' . ($link_type) . ' - ' . strip_tags(substr($result->smessage,0,30)) . ' - ' . elapsed_time($date_status) . '</a>';

非常感谢您的建议

最佳答案

将此值添加为额外字段:

 SELECT sid as ssid, created as screated, message as smessage, recipient as srecipient, "status" as link_type 
FROM {statuses}
WHERE created > :logout_stamp
UNION
SELECT sid as ssid, created as screated, comment as smessage, uid as srecipient,"comment" as link_type
FROM {fbss_comments} fbss
WHERE fbss.created > :logout_stamp
ORDER BY screated DESC LIMIT 15

关于php/mysql - 使用联合,如何根据选定的表设置变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15524764/

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