如果其中一個表為空,mysql 中的查詢給出 0 結果
P粉848442185
P粉848442185 2023-09-13 13:32:30
0
1
412

我有這 3 張表:

create table reports(id int not null AUTO_INCREMENT,name varchar(255)not null,public_access tinyint not null,primary key (id));
create table report_users(id int not null AUTO_INCREMENT,report_id int not null,user_id int not null,primary key (id),foreign key (report_id) references reports(id));
create table report_groups(id int not null AUTO_INCREMENT,report_id int not null,group_id int not null,primary key (id),foreign key (report_id) references reports(id));

我想從報表表中取得至少滿足以下條件之一的行:

1 - The field public_access is true
2 - The report is in the related table report_users with in parameter user_id 
3 - The report is in the related table report_groups with in parameter group_id

首先,我建立一個可公開存取的新報告:

insert into reports values(null, 'report 1 open to all', 1);

然後另一個報告只能由 user_id = 1 存取:

insert into reports values(null, 'report 2 only for user_id 1', 0);
insert into report_users values(null, 2, 1);

然後另一個報表只能由group_id = 1存取

insert into reports values(null, 'report 3 only for group_id 1', 0);
insert into report_groups values(null, 3, 1);

現在,我有 3 行:每一行都可以訪問,一行只能由 user_id = 1 訪問,另一行只能由 group_id = 1 訪問。

給我 user_id = 1 的所有行:

select reports.* 
from reports, report_users,report_groups
where 
reports.public_access = 1
or
(report_users.report_id = reports.id and report_users.user_id = 1)
or
(report_groups.report_id = reports.id and report_groups.group_id = 5)
;

我得到 2 行。它有效。

給我 group_id = 1 的所有行:

select reports.* 
from reports, report_users,report_groups
where 
reports.public_access = 1
or
(report_users.report_id = reports.id and report_users.user_id = 4)
or
(report_groups.report_id = reports.id and report_groups.group_id = 1)
;

我得到 2 行。它有效。

但是。如果report_users或report_groups為空,我不會得到任何結果。我首先運行此查詢:

truncate table report_groups;

當我執行與以前相同的查詢時,我得到一個空集合。為什麼?實際上,看起來我發送的 user_id 和 group_id 沒有任何區別。我總是會得到 0 行。

對我來說,似乎只是因為兩個表之一是空的,所以我沒有得到任何結果。 查詢本身有問題嗎?

P粉848442185
P粉848442185

全部回覆(1)
P粉087951442

你用這行程式碼做什麼:

from reports, report_users,report_groups

是 3 個表的(舊式)CROSS JOIN,這表示如果其中一個表為空,則結果也為空。

而是使用EXISTS

select r.* 
from reports r
where r.public_access = 1
   or exists (select * from report_users u where u.report_id = r.id and u.user_id = ?)
   or exists (select * from report_groups g where g.report_id = r.id and g.group_id = ?);
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!