gpt4 book ai didi

php - 条件 MySQL 查询

转载 作者:行者123 更新时间:2023-11-29 06:17:39 25 4
gpt4 key购买 nike

我有一个包含以下列的表:条件1、条件2、数据。如果其中任何行还包含条件 2 = 值 2,我需要返回条件 1 = 值 1 的所有行。我怎样才能做到这一点?

编辑:我的要求有些困惑。

如果您有列

`条件1 |条件2 |数据

A |一个 |

A |乙|一个`

如果条件 1 = A 且条件 2 = B,我想返回两行

编辑2:仍然存在一些困惑。

这是另一个例子:

条件1 |条件2 |数据

1 | 1 |

1 | 2 |

2 | 1 | C

2 | 2 | d

如果条件1 = 1且条件2 = 1,则应返回

1 | 1 |

1 | 2 |

如果条件1 = 2且条件2 = 1,则应返回

2 | 1 | C

2 | 2 | d

如果条件1 = 2且条件2 = 2,则应返回

2 | 1 | C

2 | 2 | d

如果条件 1 = 2 且条件 2 = 3,则不应返回任何行。

最佳答案

select *
from tbl A
where conditional1 = 'A'
and exists (
select * from tbl B
where B.conditional1 = 'A' and B.conditional2 = 'B')

或者更适合 MySQL 的版本

select * from tbl
where conditional1 in
(
select conditional1
from tbl
where conditional1 = 'A' and conditional2 = 'B'
)

关于php - 条件 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5522632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com