如何在WHERE子句中使用计算值?

时间:2019-10-25 10:17:06

标签: mysql sql phpmyadmin where-clause

我有以下SQL:

SELECT members.id, FLOOR(DATEDIFF('2019-10-25', crew_cv.dob) / 365.25) as age
FROM members
JOIN crew_cv ON members.id=crew_cv.user_id 
WHERE members.active=1 AND age>20 AND age<30 
ORDER BY crew_cv.last_name, crew_cv.first_name 
LIMIT 0,30

我在phpMySQL中收到以下错误:

#1054 - Unknown column 'age' in 'where clause'

如何在age子句中使用计算出的值WHERE

3 个答案:

答案 0 :(得分:1)

在sql中,您不能在where子句中使用别名(必须重复列代码)

parentElement

但是您可以创建一个视图

  SELECT members.id, FLOOR(DATEDIFF('2019-10-25', crew_cv.dob) / 365.25) as age
  FROM members
  JOIN crew_cv ON members.id=crew_cv.user_id 
  WHERE members.active=1 AND FLOOR(DATEDIFF('2019-10-25', crew_cv.dob) / 365.25)>20 
      AND age<FLOOR(DATEDIFF('2019-10-25', crew_cv.dob) / 365.25) 
  ORDER BY crew_cv.last_name, crew_cv.first_name 
  LIMIT 0,30

然后

create view my_view as 
select   members.id, crew_cv.last_name, crew_cv.first_name , FLOOR(DATEDIFF('2019-10-25', crew_cv.dob) / 365.25) as age
 FROM members
  JOIN crew_cv ON members.id=crew_cv.user_id 
  WHERE members.active=1

或将条件应用于Haveing子句

在sql中,线索以特定顺序进行求值
在select子句之前评估where条件(因此在此阶段不知道列别名) 相反,having子句在select子句

之后求值

答案 1 :(得分:1)

FROM clause
ON clause
OUTER clause
WHERE clause
GROUP BY clause
HAVING clause
SELECT clause
DISTINCT clause
ORDER BY clause
TOP clause

这是查询执行的顺序,这意味着在查询中(在where子句中)在定义(在select子句中)之前使用计算。

如果您仍然希望将所有内容放在一个查询中,这是我建议的查询:

SELECT members.id, temp.age
FROM members, 
     (SELECT user_id, crew_cv.last_name, crew_cv.first_name
             FLOOR(DATEDIFF('2019-10-25', crew_cv.dob) / 365.25) as age
      FROM crew_cv) as temp
WHERE members.id = temp.user_id  
      AND members.active=1
      AND temp.age > 20 AND temp.age < 30
ORDER BY temp.last_name, temp.first_name
LIMIT 0,30

答案 2 :(得分:1)

Maxim已经回答了问题,但删除了答案。

MySQL扩展了function detect_intent_texts($projectId, $text, $sessionId, $context, $parameters, $languageCode = 'en-US') { // new session $test = array('credentials' => 'client-secret.json'); $sessionsClient = new SessionsClient($test); $session = $sessionsClient->sessionName($projectId, $sessionId ?: uniqid()); //printf('Session path: %s' . PHP_EOL, $session); // create text input $textInput = new TextInput(); $textInput->setText($text); $textInput->setLanguageCode($languageCode); $contextStruct = new Struct(); $contextStruct->setFields($context['parameters']); $paramStruct = new Struct(); $paramStruct->setFields($parameters['parameters']); $contextInput = new Context(); $contextInput->setLifespanCount($context['lifespan']); $contextInput->setName($context['name']); $contextInput->setParameters($contextStruct); $queryParams = new QueryParameters(); $queryParams->setPayload($paramStruct); // create query input $queryInput = new QueryInput(); $queryInput->setText($textInput); // get response and relevant info $response = $sessionsClient->detectIntent($session, $queryInput); // Here I don't know how to send the context and payload $responseId = $response->getResponseId(); $queryResult = $response->getQueryResult(); $queryText = $queryResult->getQueryText(); $intent = $queryResult->getIntent(); $displayName = $intent->getDisplayName(); $confidence = $queryResult->getIntentDetectionConfidence(); $fulfilmentText = $queryResult->getFulfillmentText(); $returnResponse = array( 'responseId' => $responseId, 'fulfillmentText' => $fulfilmentText ); $sessionsClient->close(); return $returnResponse; } 子句,因此即使在没有聚合的查询中它也可以工作。这使它可以使用别名过滤查询-这很方便。所以:

HAVING

MySQL这样做是因为它倾向于实现子查询。这增加了读取和写入数据的开销。在大多数其他数据库中,您只需要使用子查询或CTE来表达它,而不会影响性能。 MySQL重载SELECT m.id, FLOOR(DATEDIFF('2019-10-25', c.dob) / 365.25) as age FROM members m JOIN crew_cv c ON m.id = c.user_id WHERE m.active = 1 HAVING age > 20 AND age < 30 ORDER BY c.last_name, c.first_name LIMIT 0, 30; 作为替代方法。

而且,所有这些,HAVING是一个近似值。更准确的查询是:

/ 365.25

如果MySQL认为合适的话,它还可以在SELECT m.id, FLOOR(DATEDIFF('2019-10-25', c.dob) / 365.25) as age FROM members m JOIN crew_cv c ON m.id = c.user_id WHERE m.active = 1 AND c.dob >= DATE('2019-10-25') - INTERVAL 30 YEAR AND c.dob <= DATE('2019-10-25') - INTERVAL 21 YEAR ORDER BY c.last_name, c.first_name LIMIT 0, 30; 上使用索引。