这是我的查询
select *
from players
where sport='football'
and position='DEF'
and pname!='Binoy Dalal'
and pname not in (select player1,player2,player3,player4,player5,player6,player7,player8
from team
where sap='60003100009')
order by price desc;
Run Code Online (Sandbox Code Playgroud)
没有该pname not in ...条款它工作正常。
我无法弄清楚什么是错误的原因在语法上它是正确的,因为 mysql 没有显示任何错误。
Tar*_*ryn 11
您不能在这样的子查询中返回多个列,因此您有多种方法可以重写此查询以使其工作。
您可以取消透视team表中的数据,以便只返回一列:
select *
from players
where sport='football'
and position='DEF'
and pname!='Binoy Dalal'
and pname not in (select player1
from team where sap='60003100009'
union all
select player2
from team where sap='60003100009'
union all
select player3
from team where sap='60003100009'
union all
select player4
from team where sap='60003100009'
union all
select player5
from team where sap='60003100009'
union all
select player6
from team where sap='60003100009'
union all
select player7
from team where sap='60003100009'
union all
select player8
from team where sap='60003100009')
order by price desc;
Run Code Online (Sandbox Code Playgroud)
或者您可以使用NOT EXISTS查询:
select *
from players p
where sport='football'
and position='DEF'
and pname!='Binoy Dalal'
and not exists (select *
from team t
where sap='60003100009'
AND
(
p.pname = t.player1 OR
p.pname = t.player2 OR
p.pname = t.player3 OR
p.pname = t.player4 OR
p.pname = t.player5 OR
p.pname = t.player6 OR
p.pname = t.player7 OR
p.pname = t.player8
))
order by price desc;
Run Code Online (Sandbox Code Playgroud)
或者您必须对WHERE播放器名称使用多个过滤器:
select *
from players
where sport='football'
and position='DEF'
and pname!='Binoy Dalal'
and pname not in (select player1
from team where sap='60003100009')
and pname not in (select player2
from team where sap='60003100009')
and pname not in (select player3
from team where sap='60003100009')
and pname not in (select player4
from team where sap='60003100009')
and pname not in (select player5
from team where sap='60003100009')
and pname not in (select player6
from team where sap='60003100009')
and pname not in (select player7
from team where sap='60003100009')
and pname not in (select player8
from team where sap='60003100009')
order by price desc;
Run Code Online (Sandbox Code Playgroud)
但是,理想情况下,您应该考虑对team表格进行规范化,以便有一列包含玩家姓名,另一列为他们分配玩家编号。与此类似:
create table team
(
player varchar(50),
playerNumber int
);
Run Code Online (Sandbox Code Playgroud)
然后,当您搜索团队数据时,您只需加入一列而不是 8 个不同的列。
| 归档时间: |
|
| 查看次数: |
27669 次 |
| 最近记录: |