MySQL ISNULL 与 INNER JOIN
我有 2 个表:“shares”和“pending_share_changes”。 “shares”表具有以下列:
share_ID, asset_ID, member_ID, percent_owner, is_approved
pending_share_changes 具有以下列:
share_change_ID, asset_ID, member_ID, percent_owner, is_approved, requested_by
我有一个表单(php),其中列出了成员必须批准或拒绝的待定更改。一切正常,除非该成员没有现有股份,这意味着“股份”表中没有他们的记录。我用来查看挂起的份额更改的查询如下:
SELECT p.share_change_ID, assets.asset_desc, assets.asset_value, shares.percent_owner AS
percent_owner_old, p.percent_owner
FROM pending_share_changes as p
inner join assets on assets.asset_ID = p.asset_ID
inner join shares on shares.asset_ID = p.asset_ID AND shares.member_ID = p.member_ID
INNER JOIN orgs ON orgs.org_ID = assets.org_ID
WHERE orgs.org_ID = '$org_ID' AND p.member_ID = '$member_ID' AND p.is_approved = '0'
我尝试使用 IFNULL(shares.percent_owner, 0) AS%_owner_old 但这不起作用。查询没有返回结果。如果共享表中没有记录,我希望percent_owner_old 列显示“0”。
谢谢。
I have 2 tables: "shares" and "pending_share_changes". The "shares" table has the following columns:
share_ID, asset_ID, member_ID, percent_owner, is_approved
pending_share_changes has the following columns:
share_change_ID, asset_ID, member_ID, percent_owner, is_approved, requested_by
I have a form (php) which lists the pending changes which the members have to approve or deny. Everything works fine unless the member has no existing shares, which means there is no record in the "shares" table for them. The query I am using to view the pending share changes is as follows:
SELECT p.share_change_ID, assets.asset_desc, assets.asset_value, shares.percent_owner AS
percent_owner_old, p.percent_owner
FROM pending_share_changes as p
inner join assets on assets.asset_ID = p.asset_ID
inner join shares on shares.asset_ID = p.asset_ID AND shares.member_ID = p.member_ID
INNER JOIN orgs ON orgs.org_ID = assets.org_ID
WHERE orgs.org_ID = '$org_ID' AND p.member_ID = '$member_ID' AND p.is_approved = '0'
I tried using IFNULL(shares.percent_owner, 0) AS percent_owner_old but that didn't work. The query returns no results. I would like to have the percent_owner_old column display a "0" if there is no record in the shares table.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
shares
的连接从INNER JOIN
更改为LEFT JOIN
。这意味着“对于
p
中的每个member_ID
和asset_ID
,将其连接到shares
中的匹配行。如果member_ID
/asset_ID
存在于p
中,但不存在于shares
中,无论如何都要创建一行并将相应的值设置为NULL
”。INNER JOIN
表示仅显示相关记录存在于两个表中的连接行。然后您可以与 IFNULL(shares.percent_owner,0) AS%_owner_old 结合使用。
Try changing your join to
shares
from anINNER JOIN
to aLEFT JOIN
.This means "for every
member_ID
andasset_ID
inp
, join it to the matching row inshares
. If themember_ID
/asset_ID
exists inp
but notshares
, make a row anyway and set the corresponding values toNULL
".The
INNER JOIN
means to only show joined rows where the relevant record exists in both tables.Then you can combine with the
IFNULL(shares.percent_owner,0) AS percent_owner_old
.