- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在过去的几个小时里,我一直在尝试完成以下操作,但没有成功:
$stmt = $db->query( "SELECT league_match_id as match_id, league_match_home_team as home_team, league_match_away_team as away_team FROM $table WHERE (( home_team = away_team ) AND (away_team = home_team))" );
假设我们有 team1 和 team2。在 Team1 主场和 Team2 客场的比赛中进行。另一场比赛(行)存储在 team2 主场和 team1 客场的位置。我想用一个查询选择两个团队。
没有球队在与自己比赛,我试图获得 2 行,其中反射(reflect)了 home_team 和 away_team 的值。
谁能帮助我走上正轨?
* 更新 *
我得到的返回如下:
Array
(
[0] => Array
(
[t1_id] => 26
[t1_home] => 2
[t1_away] => 1
[t2_id] => 24
[t2_home] => 1
[t2_away] => 2
)
[1] => Array
(
[t1_id] => 28
[t1_home] => 3
[t1_away] => 1
[t2_id] => 25
[t2_home] => 1
[t2_away] => 3
)
[2] => Array
(
[t1_id] => 24
[t1_home] => 1
[t1_away] => 2
[t2_id] => 26
[t2_home] => 2
[t2_away] => 1
)
[3] => Array
(
[t1_id] => 29
[t1_home] => 3
[t1_away] => 2
[t2_id] => 27
[t2_home] => 2
[t2_away] => 3
)
[4] => Array
(
[t1_id] => 25
[t1_home] => 1
[t1_away] => 3
[t2_id] => 28
[t2_home] => 3
[t2_away] => 1
)
[5] => Array
(
[t1_id] => 27
[t1_home] => 2
[t1_away] => 3
[t2_id] => 29
[t2_home] => 3
[t2_away] => 2
)
)
Array[0] 和 Array[2] 相同的地方,它们只是镜像。我可以去掉这里的重复项吗?我希望只有 Array[0] 或 Array[2]。这可能吗?
最佳答案
我怀疑是否会有“主队”与“客队”相同的行。
听起来好像您想找到匹配的两行。
根据您的查询条件,听起来您可能想要这样的东西:
SELECT t1.league_match_id AS t1_match_id
, t1.league_match_home_team AS t1_home_team
, t1.league_match_away_team AS t1_away_team
, t2.league_match_id AS t2_match_id
, t2.league_match_home_team AS t2_home_team
, t2.league_match_away_team AS t2_away_team
FROM $table t1
JOIN $table t2
ON t1.league_match_home_team = t2.league_match_away_team
AND t1.league_match_away_team = t2.league_match_home_team
这假设您在表格中有相应的行,例如
id home away
-- ----- ------
2 bears tigers
3 tigers bears
如果有多行具有相同的 (home,away),您将获得多个匹配项。例如,与:
id home away
-- ----- ------
2 bears tigers
3 tigers bears
5 tigers bears
7 tigers bears
11 bears tigers
您总共会得到十二行。 (id 值为 2 和 11 的行将分别与 id 值为 3、5 和 7 的行“匹配”。)
更新
删除重复项取决于重复项的来源。添加 DISTINCT
关键字将确保结果集中没有两行完全相同,但我怀疑您的重复问题比这更深...... bears
和老虎
在多场联赛中主客场交手。
在那种情况下,您需要在表中添加一些额外的内容,并需要一些谓词来限制匹配。这可能是日期,以及一些获取“最新日期”的方法,但这取决于表中的其他内容。
仅显示列,GROUP BY
和聚合函数(如 MAX()
)可用于为每个“匹配项”获取一个不同的行。
例如:
SELECT MAX(t1.league_match_id) AS t1_match_id
, t1.league_match_home_team AS t1_home_team
, t1.league_match_away_team AS t1_away_team
, MAX(t2.league_match_id) AS t2_match_id
, t1.league_match_away_team AS t2_home_team
, t1.league_match_home_team AS t2_away_team
FROM $table t1
JOIN $table t2
ON t1.league_match_home_team = t2.league_match_away_team
AND t1.league_match_away_team = t2.league_match_home_team
GROUP
BY t1.league_match_home_team
, t1.league_match_away_team
请注意,从 t2 返回 home
和 away
是多余的,因为 t1.home = t2.away
等等。来自 t1
和 t2
是一样的,除了 home
和 away
交换了。
为了限制“反向”行,所以你会得到 (bears,tigers)
而不是 (tigers,bears)
,你可以指定一个额外的谓词,这样你只能得到逆的一个“边”:
AND t1.league_match_home_team < t2.league_match_home_team
跟进
(我的查询中有一个拼写错误,第一个 JOIN 谓词应该在右侧指定 t2.
。我相信 OP 发现了这个问题并修复了它。)
根据最新更新,要消除结果集中的“镜像”反向行,您可以添加这样的谓词(如果您的查询有一个谓词,则在 GROUP BY
子句之后。)
HAVING t1_id < t2_id
(HAVING
子句可以引用分配给返回列的别名,这与 WHERE
子句不同。)
如果您的查询中没有 GROUP BY
,您可能会使用 WHERE 子句获得更好的性能:
WHERE t1.match_id < t2.match_id
如果获取两行中的哪一行并不重要,那么无论是小于还是大于比较都无关紧要。您选择比较 t1 和 t2 中的哪一列(“id”、“home”或“away”)并不重要,只需比较 t1 和 t2 之间保证不同的列(所以你只会看到镜子的一侧。)
关于php - 选择其中 column1 = column2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23371662/
这正确地呈现了标题,因为我可以看到一个列带有“Product ID”标题的表:。我正试图重构它,这样我就可以重用它,因为它们在整个应用程序中几乎是相同的:。在这种情况下,头不会呈现...检查页面元素中
我什至不知道如何表达这一点,但在 Python 中有没有一种方法可以引用等号之前的文本,而无需实际再次编写? ** 编辑 - 我在 Jupyter 中使用 python3 我似乎用了半辈子的时间来写作
假设我有一个包含以下列的字典 dict_ = [ {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', '
假设我有一个包含以下列的字典 dict_ = [ {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', '
假设我的数据库中有一张地铁 map ,其中每条线路的每个站点都是一行。如果我想知道我的线路在哪里互连: mysql> SELECT LineA.stop_id FROM LineA, LineB WH
代码 select c1,c2,c3,c4,c5,c6 from table where c5 in ('a', 'b') 从这里开始,我想将 c5 列拆分为两列,然后根据它们对 c
我正在尝试搜索表格中的两列,即标题和描述。但我想先搜索标题,然后再搜索描述。所以匹配标题的所有行排在第一位,所有匹配描述的行排在第二位 我可以使用单个 SQL 查询来实现吗? 最佳答案 您还可以使用
下面有以下 Python 数据框。 “标志”字段是我想要用代码创建的所需列。 我想要执行以下操作: 如果“分配类型”是预测的并且“Activities_Counter”大于 10,我想创建一个名为“F
我有两列,area 和 block,其中 area 是一个 int 类型,block 是一个 varchar。 现在我正在写两个查询: select * from table where area a
使用 Slick 2,我试图生成一个带有元组 IN 子句的查询: select * from my_table where (a, b) IN ((1, 87)); 给定: val seq: Seq[
我正在尝试从数据透视表中获取一组值,其中 A 列等于值数组,例如 ID 12 的 attribute_value_id 等于 3 和 9。这可以做到吗?我已经走了这么远... ID | post_id
我找不到这样做的有效方法。我在 Python 中有以下 DataFrame,列从 A 到 Z A B C ... Z 0 2.0 8.0 1.0 ... 5.0 1
我的数据框中有以下格式的数据: >>> df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD')) >>> df
我有多个与我公司销售的产品相关的表被新产品取代,随着时间的推移,这导致了多个表的出现。 我一遍又一遍地使用以下查询,直到最终表中只剩下 2 个产品... CREATE TABLE mar15a
我有如下 2 个表:- 表A ------------------------------- | product_id | price | --------------------
我有一个名为 tbl_mainsheet7 的表,创建方式如下: pk_mainsheet client_id project_id mainsheet_id project_cat EA_
我得到了以下 HTML 结构: ... ... 我的 CSS: #main-container { width:80%; margin:20px auto;
对于我的以下要求,我无法获得解决方案。 如果 data.table(如下)在 Col1 和 Col3 中有匹配的值。替换 Col2 值(旧的 New-Val)。 Col1 Col2 Col3
我正在通过连接几个表来构建一个 View ,以通过 Entity Framework 提取数据。由于此 View 没有唯一列,EntityFramework 无法检索正确的结果集(即第一列重复)。 为
好的,我已经尝试了太久了,是时候寻求帮助了。我有一个看起来有点像这样的数据框: person fruit quantity all_fruits 0 p1 grapes 2
我是一名优秀的程序员,十分优秀!