如何将mysql连接查询转换为Drupal 7 db_select连接查询

时间:2017-10-11 06:40:05

标签: drupal drupal-7

我想在drupal 7中将下面的mysql查询转换为db_select。

            SELECT ws.sid, u.name AS username, r.name AS role,
        DATE_FORMAT(FROM_UNIXTIME(u.created), '%d-%m-%Y') AS 'created', 
        DATE_FORMAT(FROM_UNIXTIME(u.login), '%d-%m-%Y') AS 'last_login',
        DATE_FORMAT(FROM_UNIXTIME(u.access), '%d-%m-%Y') AS 'last_accessed'  
        FROM users u
        INNER JOIN users_roles ur ON ur.uid = u.uid
        INNER JOIN role r ON r.rid = ur.rid
        INNER JOIN webform_submissions ws ON ws.uid = u.uid
        WHERE u.login <= UNIX_TIMESTAMP((NOW() - INTERVAL 1 YEAR)) AND r.name IN ('elev', 'lærer')
        ORDER BY last_login ASC


  Can anyone please suggest how to write it?

2 个答案:

答案 0 :(得分:1)

使用db_query

db_query("Your query here")->fetchAll();   //output as objects
db_query("Your query here")->fetchAll(PDO::FETCH_ASSOC); //output as arrays

用于您的SQL查询。

db_query("SELECT ws.sid, u.name AS username, r.name AS role,
        DATE_FORMAT(FROM_UNIXTIME(u.created), '%d-%m-%Y') AS 'created', 
        DATE_FORMAT(FROM_UNIXTIME(u.login), '%d-%m-%Y') AS 'last_login',
        DATE_FORMAT(FROM_UNIXTIME(u.access), '%d-%m-%Y') AS 'last_accessed'  
        FROM users u
        INNER JOIN users_roles ur ON ur.uid = u.uid
        INNER JOIN role r ON r.rid = ur.rid
        INNER JOIN webform_submissions ws ON ws.uid = u.uid
        WHERE u.login <= UNIX_TIMESTAMP((NOW() - INTERVAL 1 YEAR)) AND r.name IN ('elev', 'lærer')
        ORDER BY last_login ASC")->fetchAll(PDO::FETCH_ASSOC);

答案 1 :(得分:1)

你去吧

foreach