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

时间:2018-05-18 08:46:01

标签: mysql drupal phpmyadmin

我一直在尝试为drupal数据库中的fetch用户记录准备mysql查询,并按照以下方式准备了查询

在用户表的DB中,有一个创建列的用户,其中包含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查询有什么问题吗?

3 个答案:

答案 0 :(得分:0)

尝试使用unix timestamp值并检查是否有结果。其他方法是检查数据库中创建的unix时间戳是否正确。

检查:

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

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

select FROM_UNIXTIME(created) FROM users

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

答案 1 :(得分:0)

我已经尝试过以下查询为我工作:

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

答案 2 :(得分:0)

打开查询对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();

你必须在

之前将时间转换为秒

FOR 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);

希望这有助于你