简体   繁体   English

WHERE子句中的mysql计算结果

[英]mysql calculation results in WHERE clause

I would like to select my results using calculations inside the query and use these results to compare inside the WHERE statement. 我想使用查询中的计算来选择结果,并使用这些结果在WHERE语句中进行比较。 But somehow that does not work. 但是,这不起作用。 Guess you know why? 猜猜你知道为什么吗? Here is my code: 这是我的代码:

$statement = $pdo->prepare("SELECT * ,  `ft_lteam` -  `ht_lteam` AS  `ht2_lteam`,  
`ft_vteam` -  `ht_vteam` AS  `ht2_vteam` 
FROM  `sca` 
WHERE `ht2_lteam` > `ht2_vteam`");
$statement->execute(array('Max'));

Help would be great. 帮助会很棒。 Thanks for that! 感谢那!

You can use HAVING to filter on a computed column: 您可以使用HAVING来过滤计算列:

$statement = $pdo->prepare("SELECT * ,  `ft_lteam` -  `ht_lteam` AS  `ht2_lteam`,  
`ft_vteam` -  `ht_vteam` AS  `ht2_vteam` 
FROM  `sca` 
HAVING `ht2_lteam` > `ht2_vteam`");
$statement->execute(array('Max'));

Clear example here 这里的例子很清楚

SELECT col1,col2,col3,(col1*col2*col3) AS result, number FROM table
HAVING result > number
ORDER by result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM