0

I'm trying to add following mysql condition:

WHERE       DAYOFYEAR(curdate()) <= DAYOFYEAR(DATE_ADD(FROM_UNIXTIME(field_birthday_value, '%y-%m-%d'), INTERVAL (YEAR(NOW()) - YEAR(FROM_UNIXTIME(field_birthday_value, '%y-%m-%d'))) YEAR))
AND         DAYOFYEAR(curdate()) +7 >= DAYOFYEAR(DATE_ADD(FROM_UNIXTIME(field_birthday_value, '%y-%m-%d'), INTERVAL (YEAR(NOW()) - YEAR(FROM_UNIXTIME(field_birthday_value, '%y-%m-%d'))) YEAR))

To my db_select $query->condition:

//Build query
$query = db_select('users', 'u');
$query->leftJoin('field_data_field_birthday', 'fb', 'u.uid = fb.entity_id');
$query->condition('THE CONDITION MUST BE ADDED HERE');
$query->fields('u', array('uid', 'name'));
$query->fields('fb', array('field_birthday_value'));
$query->range(0,5);
$users = $query->execute();

But i can't figure out how. Simple conditions like =, <> are easy. But i got no clue how to add my more complex condition.

Thx

EDIT: Should pay more attention to the documentation :). Ty!

//Build query
$query = db_select('users', 'u');
$query->leftJoin('field_data_field_birthday', 'fb', 'u.uid = fb.entity_id');
$query->where("DAYOFYEAR(curdate()) <= DAYOFYEAR(DATE_ADD(FROM_UNIXTIME(fb.field_birthday_value, '%y-%m-%d'), INTERVAL (YEAR(NOW()) - YEAR(FROM_UNIXTIME(fb.field_birthday_value, '%y-%m-%d'))) YEAR))");
$query->where("DAYOFYEAR(curdate()) +7 >= DAYOFYEAR(DATE_ADD(FROM_UNIXTIME(fb.field_birthday_value, '%y-%m-%d'), INTERVAL (YEAR(NOW()) - YEAR(FROM_UNIXTIME(fb.field_birthday_value, '%y-%m-%d'))) YEAR))");
$query->fields('u', array('uid', 'name'));
$query->fields('fb', array('field_birthday_value'));
$query->range(0,5);
$users = $query->execute();

2 Answers 2

0

As The Docs for condition says:

Parameters

$field: The name of the field to check. If you would like to add a more complex condition involving operators or functions, use where().

So let's see QueryConditionInterface::where:

Parameters

$snippet: A portion of a WHERE clause as a prepared statement. It must use named placeholders, not ? placeholders.

$args: An associative array of arguments.

Should do the trick for you.

0

Your need use the

$query->where(...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.