如果不是null,我希望选择一列(comment_graduate_id
,comment_employer_id
)的数据。其中一个将是零,第二个将具有价值。因此,我想选择带有值的列以从其表中调用其数据:
$stmt = $con->prepare("
SELECT
comments.comment_id,
comments.comment_content,
comments.comment_date_and_time,
graduated.graduate_first_name,
employers.employer_name
FROM comments
INNER JOIN graduated ON
comments.comment_graduate_id = graduated.graduate_id
INNER JOIN employers ON
comments.comment_employer_id = employers.employer_id
WHERE
comments.comment_job_offer_id = ?
ORDER BY comments.comment_id DESC");
有可用的功能,我更喜欢使用 coalesce()
,因为它不是数据库特定的,并且也可以处理2个参数。
coalesce( comment_graduate_id, comment_employer_id )
这将返回comment_graduate_id如果不是null,否则它将返回comment_employer_id,如果第二列也为null,则您将返回null。
MySQL的特定功能是IFNULL()
,但我敦促您改用Cocce。