gpt4 book ai didi

mysql - 在 SQL INNER JOIN 中使用额外的 WHERE

转载 作者:可可西里 更新时间:2023-11-01 07:35:35 25 4
gpt4 key购买 nike

$sql = "SELECT events.*, leagues.lname as leagueName,leagues.aliasname as AliasName, t1.tname as team1_name, t2.tname as team2_name 
FROM events
INNER JOIN leagues ON events.leagueid = leagues.id
INNER JOIN teams AS t1 ON events.teamhid = t1.teamid
INNER JOIN teams AS t2 ON events.teamaid = t2.teamid
WHERE sdate='1352835000' ";

它给出了带有 COLUMNS 的 TABLE:

id sdate ... leagueName AliasName team1_name team2_name 

如何添加附加条件:

WHERE AliasName LIKE 'word' AND (team1_name LIKE 'word2' OR  team2_name LIKE 'word3')

最佳答案

你不能使用 ALIAS of column on (which is in-line with) where 子句,所以在你的情况下你需要使用 tableName.columnName(除了将其包装在子查询中)

WHERE leagues.aliasname LIKE 'word' AND 
(t1.tname LIKE 'word2' OR t2.tname LIKE 'word3')

之所以不能使用是因为查询的执行顺序如下

  • FROM 子句(你可以在这里设置 TABLE 别名)
  • WHERE 子句
  • GROUP BY 子句
  • HAVING子句
  • SELECT 子句(此处为 COLUMN 别名)
  • ORDER BY 子句

关于mysql - 在 SQL INNER JOIN 中使用额外的 WHERE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13362554/

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