dedecms sql查询(sql developer 查询)

随着互联网的快速发展,网站已经成为企业、个人展示形象和拓展业务的重要平台。而DedeCMS作为一款国内流行的内容管理系统,凭借其易用性、可扩展性等优势,深受广大用户的喜爱。在DedeCMS中,SQL查询扮演着至关重要的角色,它直接影响到网站的数据管理和性能表现。本文将深入解析DedeCMS SQL查询的技巧、应用与注意事项,帮助大家更好地掌握这一技能。

一、DedeCMS SQL查询基础

我们需要了解DedeCMS的数据库结构。DedeCMS通常使用MySQL数据库,其核心数据表包括:dede_archives(文章信息)、dede_arctype(文章类型)、dede_addon文章附加表(文章附加信息)等。

1. dede_archives表:存储文章的基本信息,如标题、内容、发布时间等。

字段名 数据类型 说明
aid int 文章ID
title varchar 文章标题
arcrank smallint 文章排序
ishtml tinyint 是否生成HTML
pubdate datetime 发布时间
arcrank smallint 文章排序
typeid int 文章类型ID
username varchar 发布者
onclick int 点击次数
vote smallint 投票数
votes smallint 最多投票数
voteid int 投票ID
voteip varchar 投票IP
voteip1 varchar 投票IP1
voteip2 varchar 投票IP2
votetime datetime 投票时间
votetimes int 投票次数
votestimes int 投票次数(限制)
votesid int 投票ID
votesid1 int 投票ID1
votesid2 int 投票ID2
votestimes int 投票次数
votetimes int 投票次数(限制)
votesid int 投票ID
votesid1 int 投票ID1
votesid2 int 投票ID2
votestimes int 投票次数
votetimes int 投票次数(限制)
votesid int 投票ID
votesid1 int 投票ID1
votesid2 int 投票ID2
votestimes int 投票次数
votetimes int 投票次数(限制)

2. dede_arctype表:存储文章类型信息,如名称、描述等。

字段名 数据类型 说明
id int 类型ID
typename varchar 类型名称
typedir varchar 类型目录
typeurl varchar 类型URL
typedesc text 类型描述
isdefault tinyint 是否默认类型
parentid int 父类型ID
arrsort smallint 排序
istop tinyint 是否置顶
reid int 重新排序ID
flag varchar 标记
templet varchar 模板
keywords varchar 关键词
description text 描述
lang varchar 语言
user varchar 用户
uptime datetime 更新时间

3. dede_addon文章附加表:存储文章附加信息,如缩略图、标签等。

字段名 数据类型 说明
aid int 文章ID
thumb varchar 缩略图
tag varchar 标签
tagid int 标签ID
tagname varchar 标签名称
tagtype varchar 标签类型
tagnum int 标签数量
taglink varchar 标签链接
tagurl varchar 标签URL

二、DedeCMS SQL查询技巧

1. 选择合适的字段:在编写SQL查询时,尽量只选择需要的字段,避免使用SELECT *,这样可以提高查询效率。

2. 使用索引:在DedeCMS中,很多常用字段都建立了索引,如aid、typeid等。在编写查询时,尽量使用索引字段,这样可以加快查询速度。

3. 避免使用子查询:子查询会增加数据库的负担,尽量使用连接查询替代子查询。

4. 使用LIMIT分页:在处理大量数据时,使用LIMIT实现分页可以避免一次性加载过多数据,提高页面响应速度。

5. 优化SQL语句:对SQL语句进行优化,如合并查询、去除不必要的字段等,可以提高查询效率。

三、DedeCMS SQL查询应用

1. 查询文章列表

“`sql

SELECT * FROM dede_archives WHERE typeid = 1 ORDER BY aid DESC LIMIT 0, 10;

“`

2. 查询文章详情

“`sql

SELECT * FROM dede_archives WHERE aid = 1;

“`

3. 查询文章类型

“`sql

SELECT * FROM dede_arctype WHERE id = 1;

“`

4. 查询文章标签

“`sql

SELECT * FROM dede_addon WHERE aid = 1;

“`

四、DedeCMS SQL查询注意事项

1. 数据库备份:在进行SQL查询操作之前,请确保数据库已备份,以防数据丢失。

2. 权限管理:对数据库进行权限管理,确保只有授权用户可以执行SQL查询。

3. 安全防范:防范SQL注入攻击,对用户输入进行过滤和转义。

4. 性能优化:定期对数据库进行优化,如添加索引、清理无效数据等。

掌握DedeCMS SQL查询技巧对于网站开发和维护具有重要意义。通过本文的介绍,相信大家对DedeCMS SQL查询有了更深入的了解。在实际操作中,请结合自身需求,灵活运用这些技巧,提高网站性能和用户体验。

dedecms数据库,用php调用sql最新文章

这是我之前写的一个类,和你的需求基本一样,你拿去自己改改吧

<?php

$GLOBALS['web_channel_dir']='dir/inc';

//设置系统根路径

define('WEB_ROOT',$GLOBALS['web_channel_dir']? substr(dirname(__FILE__), 0,- strlen($GLOBALS['web_channel_dir'])): dirname(__FILE__).'/');

abstract class BaseDataQuery

{

/*

*构造方法

*/

function __construct($pid,$cid,$xid)

{

if(!file_exists(WEB_ROOT.'data/common.inc.php'))

{

echo”配置错误”;

exit();

}else{

require_once WEB_ROOT.'data/common.inc.php';

}

$this->cfg_dbhost=$cfg_dbhost;

$this->cfg_dbname=$cfg_dbname;

$this->cfg_dbuser=$cfg_dbuser;

$this->cfg_dbpwd=$cfg_dbpwd;

$this->cfg_dbprefix=$cfg_dbprefix;

$this->cfg_db_language=$cfg_db_language;

$this->link=$this->connect();

}

function connect()

{

//global$link,$host,$dbuser,$dbpasswd,$db;

$link= mysql_connect($this->cfg_dbhost,$this->cfg_dbuser,$this->cfg_dbpwd) or

die(“Could not connect:”. mysql_error().””.$sql);

mysql_select_db($this->cfg_dbname);

mysql_query(“SET NAMES UTF8”);//设置数据库的要查询结果的字符集

return$link;

}

function Query($sql)

{

if(!$this->link)

$this->link=$this->connect();

$result= mysql_query($sql,$this->link) or

die(“Could not query:”. mysql_error().””.$sql);

return$result;

}

function getAll($sql)

{

$res=$this->Query($sql);

if($res!== false)

{

$arr= array();

while($row= mysql_fetch_assoc($res))

{

$arr[]=$row;

}

return$arr;

}

else

{

return false;

}

}

function getAll_cache($sql)

{

$res=$this->Query($sql);

if($res!== false)

{

$arr= array();

while($row= mysql_fetch_assoc($res))

{

$arr[]=$row;

}

return$arr;

}

else

{

return false;

}

}

function getOne($sql,$limited= false)

{

if($limited== true)

{

$sql= trim($sql.' LIMIT 1');

}

$res=$this->Query($sql);

if($res!== false)

{

$row= mysql_fetch_row($res);

if($row!== false)

{

return$row[0];

}

else

{

return'';

}

}

else

{

return false;

}

}

/**

*得到子栏目ID

*$reid上线id

*/

function get_child($reid)

{

$child_sql=”SELECT id FROM dede_arctype WHERE reid=”.$reid;

$res=$this->getAll($child_sql);

if(empty($res)){

return$reid;

}

foreach($res AS$row)

{

if(!empty($row['id']))

{

$arr[]=$row['id'];

$this->get_child($row['id']);

}

}

$str= join(“,”,$arr);

return$str;

}

/**

*得到子栏目名称与id

*$reid上级id

*/

function get_child_name($reid)

{

$child_sql=”SELECT id,typename,reid FROM dede_arctype WHERE reid=”.$reid;

$res=$this->getAll($child_sql);

return$res;

}

function get_ic_name($id)

{

$child_sql=”SELECT typename FROM dede_arctype WHERE id=”.$id;

$res=$this->getAll($child_sql);

return$res;

}

/*

*数据查询

*$flag(需要查询的标记例头条[h]推荐[c]幻灯[f]特荐[a]滚动[s]加粗[b]图片[p]跳转[j])

*$typeid需要查询的栏目id(22,23,24,25)

*$len需要查询的数据长度如果$len是数据就是limit 1,100形式如果是字符就是limit 100如果$len不存就是不限止条数

**/

function query_data($queryInfo)

{

$queryInfo['flag']?$flag=$queryInfo['flag']:$flag= false;

$typeid=$queryInfo['typeid'];

$queryInfo['len']?$len=$queryInfo['len']:$len= false;

$field=$queryInfo['field'];

$queryInfo['order']?$order=$queryInfo['order']:$order= false;

$queryInfo['channel']?$channel=$queryInfo['channel']:$channel= false;

$queryInfo['where']?$where=” AND”.$queryInfo['where']:$where=””;

$queryInfo['typeid2']?$typeid2=”AND typeid2 in(“.$queryInfo['typeid2'].”)”:$typeid2=””;

$queryInfo['table']?$table=$queryInfo['table']:$table=”dede_archives”;

//定义查询数据标识(头条,图片,推荐)

if($flag)

{

$flag=”and find_in_set('$flag',flag)”;

}else{

$flag=””;

}

//查询条数

if($len)

{

if(is_array($len))

{

$len=”limit”.$len[0].”,”.$len[1];

}else{

$len=”limit 0,”.$len;

}

}else{

$len=””;

}

//定义排序条件

if(isset($order)&& is_array($order))

{

$orderarr= array();

foreach($order as$key=>$value)

{

$orderarr[]=$key.””.$value;

}

$orderstr= join(“,”,$orderarr);

}

elseif($order=='rand')

{

$orderstr=” rand(),id desc”;

}

elseif($order!='')

{

$orderstr=$order;

}

else

{

$orderstr=”pubdate desc”;

//$orderstr=$order;

}

//定义查询模型

if($channel)

{

$channel=”AND channel=”.$channel;

}elseif($channel==”null”){

$channel=””;

}

else

{

$channel=”AND channel= 1″;

}

//定义查询字段

if($field)

{

if(is_array($field))

{

$field= join(“,”,$field);

}

$sql=”select”.$field.” from”.$table.” WHERE arcrank>-1 and typeid in(“.$typeid.”)”.$typeid2.$flag.””.$channel.”$where ORDER BY”.$orderstr.””.$len;

}else{

//根剧栏目查询新闻

$sql=”select id,title,pubdate from”.$table.” WHERE arcrank>-2 and typeid in(“.$typeid.”)”.$typeid2.$flag.””.$channel.”$where ORDER BY”.$orderstr.””.$len;

//echo”<br/>”;

}

if($queryInfo['debug']){

$debugstr=”this is Debug:[$sql ]”;

return$debugstr;

}

$result=$this->getAll($sql);

return$result;

}

}

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享