Mysql 连接两个表时可以使用或吗?

Mysql 连接两个表时可以使用或吗?,mysql,sql,database,relational-database,Mysql,Sql,Database,Relational Database,在这种情况下,我试图在发出通知的员工的用户名旁边检索通知消息。问题是,如果通知不是由员工发送的(而是由企业所有者自己发送的),则通知表中的“业务”帐户id的通知可能为0。如果由业务所有者发送,则不会有用户名,因此我的查询不会检索这些通知 我的目标是检索business id 5的所有通知。如果Notifications.notification_by_business_account_id设置为0(表示通知由所有者发送),我只想将account_username列中的值留空。我该怎么做?我会在什

在这种情况下,我试图在发出通知的员工的用户名旁边检索通知消息。问题是,如果通知不是由员工发送的(而是由企业所有者自己发送的),则通知表中的“业务”帐户id的通知可能为0。如果由业务所有者发送,则不会有用户名,因此我的查询不会检索这些通知

我的目标是检索business id 5的所有通知。如果Notifications.notification_by_business_account_id设置为0(表示通知由所有者发送),我只想将account_username列中的值留空。我该怎么做?我会在什么地方用电话吗?这是我的桌子:

SELECT Notifications.message, BusinessAccounts.employee_username
FROM Notifications, BusinessAccounts
WHERE Notifications.notification_by_business_account_id = BusinessAccounts.account_id AND Notifications.notification_business_id=5

有多种业务。我现在正在测试id为5的业务。

这是左侧外部联接的教科书示例,因此我建议您仔细阅读联接,尤其是外部联接。注意“外部”一词并非总是在代码中拼写出来,但“左连接”与“左外部连接”相同

问题是:

TABLE `Notifications` (
  `notification_id` mediumint(8) unsigned NOT NULL,
  `notification_business_id` mediumint(8) unsigned NOT NULL,
  `notification_by_business_account_id` int(10) unsigned NOT NULL COMMENT 'This id represents the id of the employee account that sent the notification. Set 0 if from business owner.',
  `notification_message` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL,
  `notification_date_sent` datetime NOT NULL
)

TABLE `BusinessAccounts` (
  `account_id` int(10) unsigned NOT NULL,
  `account_username` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `account_business_id` mediumint(8) unsigned NOT NULL
)
Select *
From Notifications As N
Left Outer Join BusinessAccounts As B
    On      N.notification_by_business_account_id = B.account_id 
Where N.notification_business_id In (5 /*List your business ID's separated by commas here if/when you have multiple*/)