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
//更新操作
<?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
//查询操作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
相关文章推荐
- MySQL性能优化经验
- MYSQL错误代码大全
- 数据库_MySQL_MySQL日期转换函数
- Mysql中的date与datetime,以及如何向Mysql中插入“日期+时间”数据
- 查看Mysql实时执行的Sql语句
- mysql 命令行操作
- 启动mysql出现1067错误
- MySQL字符集编码
- mysql 5.7.11 安装运行
- MySQL数据库优化
- win7中mysql安装
- mysql中的事件计划(event scheduler)
- MySQL内存调优
- 4.1 MySQL程序概述
- mysql数据迁移
- mysql中的view(虚拟表)
- mysql 之 主从加 keepalive 在生产系统应用(zabbix)
- 数据库_基础知识_MySQL_UpdateSelect(根据查询出来的结果批量更新)
- MySql定时器的那点事
- mysql 相关