假设我们有一个这样的表:
**tablename**
ID Name Size Date AtStore1 AtStore2
1 Apple Medium 20120101 Yes
2 Pear Medium 20111231 Yes Yes
3 Lemon Small 20111231 Yes Yes
4 Orange Small 20111231 Yes
5 Carrot Medium 20111231 Yes
6 Potato Small 20111231 Yes
7 Celery Large 20111231 Yes
8 Onion Medium 20111231
9 Tomato Medium 20111231
10 Apple Medium 20111231
12 Pear Medium 20111230 Yes Yes
13 Lemon Small 20111230 Yes
14 Orange Small 20111230 Yes
15 Carrot Medium 20111230 Yes
16 Potato Small 20111230
17 Celery Large 20111229
18 Onion Medium 20111229 Yes
19 Tomato Medium 20111229
我们是否可以构建一个高效的查询来检索每行的所有数据,其中:
- “是”AtStore1 行,其中当天的 AtStore1 中至少有 4 个“是”,
或者(含) - AtStore2 中有一个“是”
如果只满足第一个参数,即行的正向查找,也是可以接受的。如有必要,我可能可以使用 PHP 编写 AtStore2 部分的脚本。我所有的尝试都惨遭失败;我对如何在 Google 中有效地写出问题的尝试也没有什么成果。
(在 PHP 中将事情分解为不同代码部分的答案也很好,我只想要一些相当有效的东西。)
对于此示例表,这些将是预期的行:
ID Name Size Date AtStore1 AtStore2
1 Apple Medium 20120101 Yes
2 Pear Medium 20111231 Yes Yes
3 Lemon Small 20111231 Yes Yes
4 Orange Small 20111231 Yes
5 Carrot Medium 20111231 Yes
6 Potato Small 20111231 Yes
7 Celery Large 20111231 Yes
12 Pear Medium 20111230 Yes Yes
14 Orange Small 20111230 Yes
15 Carrot Medium 20111230 Yes
18 Onion Medium 20111229 Yes
如你看到的
- 2012 年 1 月 1 日
-AtStore1 中没有 Yeses
-AtStore2 有 Yes 所以返回该行 - 12 月 31 日
-5 AtStore1 中的 Yeses,因此返回(ID 2 和 3 中的 AtStore2 Yeses 也足够了)
-也有 ID 7,因为 AtStore2 中有 Yes - 返回的其余行仅是因为 AtStore2 中的 Yeses