您的位置:首页 > 数据库 > MySQL

drupal mysql

2016-02-26 14:13 609 查看
drupal中的MySQL的操作:

  //查询操作select

  $query = db_select ( 'field_data_field_article_cata_first', 'f' );

  $query->leftjoin ( 'node', 'n', 'n.nid=f.entity_id' );

  $query->fields ( 'f', array (

      'field_article_cata_first_tid'

  ) );

  //别名

  $query->addExpression ( 'max(f.entity_id)', 'max' );

  $query->condition ( 'n.uid', "{$userId}", '=' );

  $query->groupBy ( 'f.field_article_cata_first_tid' );

  $query = $sql->extend ( 'PagerDefault' )->limit ( 5 );

   $query = $sql->execute()->fetchAll();

    //有时也可以

    $sql="select * from node n left join field_data_field_og_catalog f on n.nid=f.entity_id";

    $sql.=" WHERE f.field_og_catalog_tid=14 order by n.created DESC LIMIT 3";

    $result = db_query($sql)->fetchAll();

    

   //插入操作insert 

// For the following query:
// INSERT INTO {node} (title, uid, created) VALUES ('Example', 1, 1221717405)

$nid = db_insert('node') // Table name no longer needs {}
->fields(array(
  'title' => 'Example',
  'uid' => 1,
  'created' => REQUEST_TIME,
))
->execute();

// Above Example is Equivalent to the Following in D6
$result = db_query("INSERT INTO {node} (title, uid, created) VALUES (%s, %d, %d)", 'Example', 1, time());

// OR using drupal_write_record...
$data = array(
  'title' => 'Example',
  'uid' => 1,
  'created' => REQUEST_TIME,
);
drupal_write_record('node', $data);

  //更新操作

<?php
// For the following query:

// UPDATE {node} SET uid=5, status=1 WHERE created >= 1221717405$num_updated
= db_update('node')
// Table name no longer needs {}

  ->fields(array(

    'uid' => 5,

    'status' => 1,

  ))

  ->condition('created',
REQUEST_TIME - 3600,
'>=')

  ->execute();// Above Example is Equivalent to the Following in D6
$result = db_query("UPDATE {node} SET uid = %d, status = %d WHERE created >= %d",
5, 1,
time() - 3600);?>

  //删除操作

  <?php// Drupal 7
$nid = 5;
$num_deleted = db_delete('node')

  ->condition('nid',$nid)

  ->execute();// Above example is equivalent to the following in Drupal 6
$nid = 5;
db_query("DELETE FROM {node} WHERE nid = %d",$nid);?>

//durpal.org uri: https://api.drupal.org/api/drupal/includes%21database%21database.inc/function/db_delete/7
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: