SQL:How 我可以忽略数据库和 return 行中的 NULL 或 0 值吗?
SQL:How can I ignore NULL or 0 values in database and return rows?
我有一个 MySQL table,其中某些行的某些值为 NULL 或 0 (None),我想 select 它们和 return 他们到我的 PHP 代码以及非 NULL 或非零行。
因此,我想 return 所有现有行,无论它们是 null 还是零。我想向在数据库中找到 0 (None) 或 NULL 的用户显示数字 0,而不是省略找到上述值的行,因为并非所有列都是 0 或 NULL。
这是我目前正在使用的查询;
SELECT ops_files.file_id, ops_files.file_name, ops_files.description, ops_file_cats.file_cat,
ops_file_status.status, ops_files.date_of_instructions, bd_orgs.name org1, bd_orgs2.name org2, bd_clients.name client1, bd_clients2.name client2,
bd_clients3.name client3, bd_clients4.name client4, bd_clients5.name client5, ops_int_parties.name int_party1, ops_int_parties2.name int_party2 FROM ops_files
INNER JOIN ops_file_cats ON ops_files.file_cat = ops_file_cats.cat_id
INNER JOIN ops_file_status ON ops_files.file_status = ops_file_status.status_id
INNER JOIN bd_orgs ON ops_files.org1 = bd_orgs.org_id
INNER JOIN bd_orgs bd_orgs2 ON ops_files.org2 = bd_orgs2.org_id
INNER JOIN bd_clients ON ops_files.contact_person1 = bd_clients.client_id
INNER JOIN bd_clients bd_clients2 ON ops_files.contact_person2 = bd_clients2.client_id
INNER JOIN bd_clients bd_clients3 ON ops_files.contact_person3 = bd_clients3.client_id
INNER JOIN bd_clients bd_clients4 ON ops_files.contact_person4 = bd_clients4.client_id
INNER JOIN bd_clients bd_clients5 ON ops_files.contact_person5 = bd_clients5.client_id
INNER JOIN ops_int_parties ON ops_files.int_party1 = ops_int_parties.id
INNER JOIN ops_int_parties ops_int_parties2 ON ops_files.int_party2 = ops_int_parties2.id ORDER BY file_id ASC
这是 MySQL table 之一的快照。
注意:我特意删除了附图中的一些值。
更新:
我正在尝试执行@scaisEdge 建议的内容,但每次尝试将 IFNULL 与别名一起使用时都会出现错误。如何在下面的查询中为 bd_orgs.name org1
列一直到 ops_int_parties2.name int_party2
正确实施 IFNULL?苦思冥想几个小时都无济于事。
SELECT ops_files.file_id,
IFNULL(ops_files.file_name,0) file_name,
IFNULL(ops_files.description,0) description,
IFNULL(ops_file_cats.file_cat,0) file_cat,
IFNULL(ops_file_status.status,0) status,
IFNULL(ops_files.date_of_instructions,0) date_of_instructions,
bd_orgs.name org1,
bd_orgs2.name org2,
bd_clients.name client1,
bd_clients2.name client2,
bd_clients3.name client3,
bd_clients4.name client4,
bd_clients5.name client5,
ops_int_parties.name int_party1,
ops_int_parties2.name int_party2
FROM ops_files
如果你想要 NULL 值 return 0 你可以尝试使用 ifnull(your_column, 0)
SELECT ops_files.file_id
, ifnull(ops_files.file_name,0) file_name
, ifnull(ops_files.description,0) description
......
......
FROM ops_files
INNER JOIN ops_file_cats ON ops_files.file_cat = ops_file_cats.cat_id
INNER JOIN ops_file_status ON ops_files.file_status = ops_file_status.status_id
INNER JOIN bd_orgs ON ops_files.org1 = bd_orgs.org_id
INNER JOIN bd_orgs bd_orgs2 ON ops_files.org2 = bd_orgs2.org_id
INNER JOIN bd_clients ON ops_files.contact_person1 = bd_clients.client_id
INNER JOIN bd_clients bd_clients2 ON ops_files.contact_person2 = bd_clients2.client_id
INNER JOIN bd_clients bd_clients3 ON ops_files.contact_person3 = bd_clients3.client_id
INNER JOIN bd_clients bd_clients4 ON ops_files.contact_person4 = bd_clients4.client_id
INNER JOIN bd_clients bd_clients5 ON ops_files.contact_person5 = bd_clients5.client_id
INNER JOIN ops_int_parties ON ops_files.int_party1 = ops_int_parties.id
INNER JOIN ops_int_parties ops_int_parties2 ON ops_files.int_party2 = ops_int_parties2.id ORDER BY file_id ASC
如果列涉及连接,请使用 LEFT JOIN
SELECT ops_files.file_id
, ifnull(ops_files.file_name,0) file_name
, ifnull(ops_files.description,0) description
......
......
FROM ops_files
LEFT JOIN ops_file_cats ON ops_files.file_cat = ops_file_cats.cat_id
LEFT JOIN ops_file_status ON ops_files.file_status = ops_file_status.status_id
LEFT JOIN bd_orgs ON ops_files.org1 = bd_orgs.org_id
LEFT JOIN bd_orgs bd_orgs2 ON ops_files.org2 = bd_orgs2.org_id
LEFT JOIN bd_clients ON ops_files.contact_person1 = bd_clients.client_id
LEFT JOIN bd_clients bd_clients2 ON ops_files.contact_person2 = bd_clients2.client_id
LEFT JOIN bd_clients bd_clients3 ON ops_files.contact_person3 = bd_clients3.client_id
LEFT JOIN bd_clients bd_clients4 ON ops_files.contact_person4 = bd_clients4.client_id
LEFT JOIN bd_clients bd_clients5 ON ops_files.contact_person5 = bd_clients5.client_id
LEFT JOIN ops_int_parties ON ops_files.int_party1 = ops_int_parties.id
LEFT JOIN ops_int_parties ops_int_parties2 ON ops_files.int_party2 = ops_int_parties2.id ORDER BY file_id ASC
在您的查询中,您将 select 子句引用到表 ops_file_cats、ops_file_status、bd_orgs、bd_orgs2 等中包含的列。 ....
但是您还没有加入这些表,请尝试正确添加加入
SELECT ops_files.file_id,
IFNULL(ops_files.file_name,0) file_name,
IFNULL(ops_files.description,0) description,
IFNULL(ops_file_cats.file_cat,0) file_cat,
IFNULL(ops_file_status.status,0) status,
IFNULL(ops_files.date_of_instructions,0) date_of_instructions,
bd_orgs.name org1,
bd_orgs2.name org2,
bd_clients.name client1,
bd_clients2.name client2,
bd_clients3.name client3,
bd_clients4.name client4,
bd_clients5.name client5,
ops_int_parties.name int_party1,
ops_int_parties2.name int_party2
FROM ops_files
INNER JOIN ops_file_cats ON ops_files.file_cat = ops_file_cats.cat_id
INNER JOIN ops_file_status ON ops_files.file_status = ops_file_status.status_id
INNER JOIN bd_orgs ON ops_files.org1 = bd_orgs.org_id
INNER JOIN bd_orgs bd_orgs2 ON ops_files.org2 = bd_orgs2.org_id
INNER JOIN bd_clients ON ops_files.contact_person1 = bd_clients.client_id
INNER JOIN bd_clients bd_clients2 ON ops_files.contact_person2 = bd_clients2.client_id
INNER JOIN bd_clients bd_clients3 ON ops_files.contact_person3 = bd_clients3.client_id
INNER JOIN bd_clients bd_clients4 ON ops_files.contact_person4 = bd_clients4.client_id
INNER JOIN bd_clients bd_clients5 ON ops_files.contact_person5 = bd_clients5.client_id
INNER JOIN ops_int_parties ON ops_files.int_party1 = ops_int_parties.id
INNER JOIN ops_int_parties ops_int_parties2 ON ops_files.int_party2 = ops_int_parties2.id ORDER BY file_id ASC
或者使用左连接你也需要部分匹配
SELECT ops_files.file_id,
IFNULL(ops_files.file_name,0) file_name,
IFNULL(ops_files.description,0) description,
IFNULL(ops_file_cats.file_cat,0) file_cat,
IFNULL(ops_file_status.status,0) status,
IFNULL(ops_files.date_of_instructions,0) date_of_instructions,
bd_orgs.name org1,
bd_orgs2.name org2,
bd_clients.name client1,
bd_clients2.name client2,
bd_clients3.name client3,
bd_clients4.name client4,
bd_clients5.name client5,
ops_int_parties.name int_party1,
ops_int_parties2.name int_party2
FROM ops_files
LEFT JOIN ops_file_cats ON ops_files.file_cat = ops_file_cats.cat_id
LEFT JOIN ops_file_status ON ops_files.file_status = ops_file_status.status_id
LEFT JOIN bd_orgs ON ops_files.org1 = bd_orgs.org_id
LEFT JOIN bd_orgs bd_orgs2 ON ops_files.org2 = bd_orgs2.org_id
LEFT JOIN bd_clients ON ops_files.contact_person1 = bd_clients.client_id
LEFT JOIN bd_clients bd_clients2 ON ops_files.contact_person2 = bd_clients2.client_id
LEFT JOIN bd_clients bd_clients3 ON ops_files.contact_person3 = bd_clients3.client_id
LEFT JOIN bd_clients bd_clients4 ON ops_files.contact_person4 = bd_clients4.client_id
LEFT JOIN bd_clients bd_clients5 ON ops_files.contact_person5 = bd_clients5.client_id
LEFT JOIN ops_int_parties ON ops_files.int_party1 = ops_int_parties.id
LEFT JOIN ops_int_parties ops_int_parties2 ON ops_files.int_party2 = ops_int_parties2.id ORDER BY file_id ASC
最后在连接子句中,如果您使用别名重新映射列名,则必须重复有效代码
INNER JOIN ops_file_cats ON ifnull(ops_files.file_cat,0) = ifnull(ops_file_cats.cat_id ,0)
我有一个 MySQL table,其中某些行的某些值为 NULL 或 0 (None),我想 select 它们和 return 他们到我的 PHP 代码以及非 NULL 或非零行。
因此,我想 return 所有现有行,无论它们是 null 还是零。我想向在数据库中找到 0 (None) 或 NULL 的用户显示数字 0,而不是省略找到上述值的行,因为并非所有列都是 0 或 NULL。
这是我目前正在使用的查询;
SELECT ops_files.file_id, ops_files.file_name, ops_files.description, ops_file_cats.file_cat,
ops_file_status.status, ops_files.date_of_instructions, bd_orgs.name org1, bd_orgs2.name org2, bd_clients.name client1, bd_clients2.name client2,
bd_clients3.name client3, bd_clients4.name client4, bd_clients5.name client5, ops_int_parties.name int_party1, ops_int_parties2.name int_party2 FROM ops_files
INNER JOIN ops_file_cats ON ops_files.file_cat = ops_file_cats.cat_id
INNER JOIN ops_file_status ON ops_files.file_status = ops_file_status.status_id
INNER JOIN bd_orgs ON ops_files.org1 = bd_orgs.org_id
INNER JOIN bd_orgs bd_orgs2 ON ops_files.org2 = bd_orgs2.org_id
INNER JOIN bd_clients ON ops_files.contact_person1 = bd_clients.client_id
INNER JOIN bd_clients bd_clients2 ON ops_files.contact_person2 = bd_clients2.client_id
INNER JOIN bd_clients bd_clients3 ON ops_files.contact_person3 = bd_clients3.client_id
INNER JOIN bd_clients bd_clients4 ON ops_files.contact_person4 = bd_clients4.client_id
INNER JOIN bd_clients bd_clients5 ON ops_files.contact_person5 = bd_clients5.client_id
INNER JOIN ops_int_parties ON ops_files.int_party1 = ops_int_parties.id
INNER JOIN ops_int_parties ops_int_parties2 ON ops_files.int_party2 = ops_int_parties2.id ORDER BY file_id ASC
这是 MySQL table 之一的快照。
注意:我特意删除了附图中的一些值。
更新:
我正在尝试执行@scaisEdge 建议的内容,但每次尝试将 IFNULL 与别名一起使用时都会出现错误。如何在下面的查询中为 bd_orgs.name org1
列一直到 ops_int_parties2.name int_party2
正确实施 IFNULL?苦思冥想几个小时都无济于事。
SELECT ops_files.file_id,
IFNULL(ops_files.file_name,0) file_name,
IFNULL(ops_files.description,0) description,
IFNULL(ops_file_cats.file_cat,0) file_cat,
IFNULL(ops_file_status.status,0) status,
IFNULL(ops_files.date_of_instructions,0) date_of_instructions,
bd_orgs.name org1,
bd_orgs2.name org2,
bd_clients.name client1,
bd_clients2.name client2,
bd_clients3.name client3,
bd_clients4.name client4,
bd_clients5.name client5,
ops_int_parties.name int_party1,
ops_int_parties2.name int_party2
FROM ops_files
如果你想要 NULL 值 return 0 你可以尝试使用 ifnull(your_column, 0)
SELECT ops_files.file_id
, ifnull(ops_files.file_name,0) file_name
, ifnull(ops_files.description,0) description
......
......
FROM ops_files
INNER JOIN ops_file_cats ON ops_files.file_cat = ops_file_cats.cat_id
INNER JOIN ops_file_status ON ops_files.file_status = ops_file_status.status_id
INNER JOIN bd_orgs ON ops_files.org1 = bd_orgs.org_id
INNER JOIN bd_orgs bd_orgs2 ON ops_files.org2 = bd_orgs2.org_id
INNER JOIN bd_clients ON ops_files.contact_person1 = bd_clients.client_id
INNER JOIN bd_clients bd_clients2 ON ops_files.contact_person2 = bd_clients2.client_id
INNER JOIN bd_clients bd_clients3 ON ops_files.contact_person3 = bd_clients3.client_id
INNER JOIN bd_clients bd_clients4 ON ops_files.contact_person4 = bd_clients4.client_id
INNER JOIN bd_clients bd_clients5 ON ops_files.contact_person5 = bd_clients5.client_id
INNER JOIN ops_int_parties ON ops_files.int_party1 = ops_int_parties.id
INNER JOIN ops_int_parties ops_int_parties2 ON ops_files.int_party2 = ops_int_parties2.id ORDER BY file_id ASC
如果列涉及连接,请使用 LEFT JOIN
SELECT ops_files.file_id
, ifnull(ops_files.file_name,0) file_name
, ifnull(ops_files.description,0) description
......
......
FROM ops_files
LEFT JOIN ops_file_cats ON ops_files.file_cat = ops_file_cats.cat_id
LEFT JOIN ops_file_status ON ops_files.file_status = ops_file_status.status_id
LEFT JOIN bd_orgs ON ops_files.org1 = bd_orgs.org_id
LEFT JOIN bd_orgs bd_orgs2 ON ops_files.org2 = bd_orgs2.org_id
LEFT JOIN bd_clients ON ops_files.contact_person1 = bd_clients.client_id
LEFT JOIN bd_clients bd_clients2 ON ops_files.contact_person2 = bd_clients2.client_id
LEFT JOIN bd_clients bd_clients3 ON ops_files.contact_person3 = bd_clients3.client_id
LEFT JOIN bd_clients bd_clients4 ON ops_files.contact_person4 = bd_clients4.client_id
LEFT JOIN bd_clients bd_clients5 ON ops_files.contact_person5 = bd_clients5.client_id
LEFT JOIN ops_int_parties ON ops_files.int_party1 = ops_int_parties.id
LEFT JOIN ops_int_parties ops_int_parties2 ON ops_files.int_party2 = ops_int_parties2.id ORDER BY file_id ASC
在您的查询中,您将 select 子句引用到表 ops_file_cats、ops_file_status、bd_orgs、bd_orgs2 等中包含的列。 .... 但是您还没有加入这些表,请尝试正确添加加入
SELECT ops_files.file_id,
IFNULL(ops_files.file_name,0) file_name,
IFNULL(ops_files.description,0) description,
IFNULL(ops_file_cats.file_cat,0) file_cat,
IFNULL(ops_file_status.status,0) status,
IFNULL(ops_files.date_of_instructions,0) date_of_instructions,
bd_orgs.name org1,
bd_orgs2.name org2,
bd_clients.name client1,
bd_clients2.name client2,
bd_clients3.name client3,
bd_clients4.name client4,
bd_clients5.name client5,
ops_int_parties.name int_party1,
ops_int_parties2.name int_party2
FROM ops_files
INNER JOIN ops_file_cats ON ops_files.file_cat = ops_file_cats.cat_id
INNER JOIN ops_file_status ON ops_files.file_status = ops_file_status.status_id
INNER JOIN bd_orgs ON ops_files.org1 = bd_orgs.org_id
INNER JOIN bd_orgs bd_orgs2 ON ops_files.org2 = bd_orgs2.org_id
INNER JOIN bd_clients ON ops_files.contact_person1 = bd_clients.client_id
INNER JOIN bd_clients bd_clients2 ON ops_files.contact_person2 = bd_clients2.client_id
INNER JOIN bd_clients bd_clients3 ON ops_files.contact_person3 = bd_clients3.client_id
INNER JOIN bd_clients bd_clients4 ON ops_files.contact_person4 = bd_clients4.client_id
INNER JOIN bd_clients bd_clients5 ON ops_files.contact_person5 = bd_clients5.client_id
INNER JOIN ops_int_parties ON ops_files.int_party1 = ops_int_parties.id
INNER JOIN ops_int_parties ops_int_parties2 ON ops_files.int_party2 = ops_int_parties2.id ORDER BY file_id ASC
或者使用左连接你也需要部分匹配
SELECT ops_files.file_id,
IFNULL(ops_files.file_name,0) file_name,
IFNULL(ops_files.description,0) description,
IFNULL(ops_file_cats.file_cat,0) file_cat,
IFNULL(ops_file_status.status,0) status,
IFNULL(ops_files.date_of_instructions,0) date_of_instructions,
bd_orgs.name org1,
bd_orgs2.name org2,
bd_clients.name client1,
bd_clients2.name client2,
bd_clients3.name client3,
bd_clients4.name client4,
bd_clients5.name client5,
ops_int_parties.name int_party1,
ops_int_parties2.name int_party2
FROM ops_files
LEFT JOIN ops_file_cats ON ops_files.file_cat = ops_file_cats.cat_id
LEFT JOIN ops_file_status ON ops_files.file_status = ops_file_status.status_id
LEFT JOIN bd_orgs ON ops_files.org1 = bd_orgs.org_id
LEFT JOIN bd_orgs bd_orgs2 ON ops_files.org2 = bd_orgs2.org_id
LEFT JOIN bd_clients ON ops_files.contact_person1 = bd_clients.client_id
LEFT JOIN bd_clients bd_clients2 ON ops_files.contact_person2 = bd_clients2.client_id
LEFT JOIN bd_clients bd_clients3 ON ops_files.contact_person3 = bd_clients3.client_id
LEFT JOIN bd_clients bd_clients4 ON ops_files.contact_person4 = bd_clients4.client_id
LEFT JOIN bd_clients bd_clients5 ON ops_files.contact_person5 = bd_clients5.client_id
LEFT JOIN ops_int_parties ON ops_files.int_party1 = ops_int_parties.id
LEFT JOIN ops_int_parties ops_int_parties2 ON ops_files.int_party2 = ops_int_parties2.id ORDER BY file_id ASC
最后在连接子句中,如果您使用别名重新映射列名,则必须重复有效代码
INNER JOIN ops_file_cats ON ifnull(ops_files.file_cat,0) = ifnull(ops_file_cats.cat_id ,0)