从drupal数据库用户表获取记录

k2arahey  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(356)

我一直在尝试准备mysql查询,以便从drupal数据库获取用户记录,我已经按照下面的步骤准备好了查询
在用户表中的db中,有一个用户的created列包含unix时间戳。

SELECT * FROM users WHERE  `created` BETWEEN 
UNIX_TIMESTAMP(STR_TO_DATE('March 31 2017 00:00AM', '%M %d %Y %h:%i%p')) AND 
UNIX_TIMESTAMP(STR_TO_DATE('March 31 2017 12:00PM', '%M %d %Y %h:%i%p'))

我也试过下面的查询,但没有结果

SELECT users.name AS users_name, users.uid AS uid, users.mail AS users_mail, 
users.created AS users_created FROM users as users  WHERE 
users.created BETWEEN UNIX_TIMESTAMP( (  'Mar 31 2017 00:00:00' ) ) AND 
                      UNIX_TIMESTAMP( (  'April 1 2017 00:00:00' ) )
ORDER BY users_created ASC

当我从mysql运行这个查询时,我没有得到任何结果,而db中存在多个记录。
有谁能告诉我mysql查询有什么问题吗?

0tdrvxhp

0tdrvxhp1#

我已经试过了,下面的查询为我工作:

SELECT users.name AS users_name, users.uid AS uid, users.mail AS users_mail, users.created AS users_created
FROM users as users 
WHERE 
       users.created BETWEEN UNIX_TIMESTAMP( (  '2016-03-31 00:00:00' ) ) AND 
                              UNIX_TIMESTAMP( (  '2016-04-01 00:00:00' ) )
ORDER BY users_created ASC
m0rkklqb

m0rkklqb2#

尝试使用unix timestamp值,然后检查是否有结果。另一种方法是检查在数据库中创建的unix时间戳是否正确。
支票:

SELECT * FROM users WHERE  `created` BETWEEN 1488322800 AND 1488323520

如果你没有结果,回到简单的检查

select FROM_UNIXTIME(created) FROM users

你可以控制结果,看看你是否应该有匹配

qq24tv8q

qq24tv8q3#

开放式查询对drupal不利
使用drupal api

$start_time = '1403564400';
$end_time = '1403650800';
$query = db_select('users', 'u')
->fields('n', array('created'))
->condition('created', array($start_time, $end_time), 'BETWEEN');
$result = $query->execute();
$record = $result->fetchAll();

你必须把时间转换成秒
对于drupal 8

$connection = \Drupal\Core\Database\Database::getConnection();
$users = $connection->select('users', 'u')
    ->fields('u',['created'])
    ->condition('created', [$start_time, $end_time], 'BETWEEN');
$executed = $users->execute();
$results = $executed->fetchAllAssoc(PDO::FETCH_OBJ);

希望这对你有帮助

相关问题