SupeSite常用sql语句高级篇(二)

一、资讯
1、查资讯自定义字段

SELECT i.*,n.*,c.type AS c_type,c.name AS c_name,c.displayorder AS c_displayorder,c.customfieldtext AS c_customfieldtext,c.isdefault AS c_isdefault,c.isshare AS c_isshare FROM supe_spaceitems i, supe_spacenews n, supe_customfields c WHERE i.itemid = n.itemid AND n.customfieldid = c.customfieldid AND n.customfieldid !=0

通过supe_spaceitems,supe_spacenews,supe_customfields三表联查来取得数据。

2、查资讯列表,同时查对应的图片附件

SELECT * FROM supe_spaceitems i, supe_attachments a WHERE i.picid = a.aid

查资讯列表和内容,同时查对应的图片附件

SELECT i.*, n.message, a.* FROM supe_spaceitems i, supe_spacenews n, supe_attachments a WHERE i.picid = a.aid AND i.itemid=n.itemid

3、资讯列表,同时查对应的图片附件(列出仅含图片的文章)

SELECT * FROM supe_spaceitems i, supe_attachments a WHERE i.picid = a.aid LIMIT 0,10

4、资讯列表和内容,同时查对应的图片附件

SELECT i.*, n.message, a.* FROM supe_spaceitems i, supe_spacenews n, supe_attachments a WHERE i.picid = a.aid AND i.itemid=n.itemid LIMIT 0,10

5、利用left join联合查询,得到所有文章(不含图片的输出NULL),比嵌套查询效率更高。

SELECT i . * , n.message, a . *
FROM supe_spaceitems i
INNER JOIN supe_spacenews n ON i.itemid = n.itemid
LEFT JOIN supe_attachments a ON i.picid = a.aid
ORDER BY i.itemid DESC
LIMIT 0 , 10

二、模型
1、调用模型中图片,标题,内容数据

SELECT * FROM supe_[模型英文ID]message m,supe_[模型英文ID]items i WHERE m.itemid=i.itemid

supe_[模型英文ID]message 与 supe_[模型英文ID]items 是存储模型信息的主表,添加的字段也会添加在 supe_[模型英文ID]message 里面,将这两个表联起来就可以查询出想要的大部分数据。

2.模型分类:

select g.* from supe_channels c right join supe_categories g on  c.nameid=g.type where c.upnameid!="news" and c.nameid="test"

三、论坛
1、查询论坛中今天发帖总数与论坛总帖数

SELECT sum(posts) AS posts , sum(todayposts) AS todayposts FROM cdb_forums

2、查询论坛主题,在设置主题分类时,显示出主题分类名称

SELECT * FROM 数据库名.cdb_threads t, 数据库名.cdb_threadtypes y WHERE t.typeid = y.typeid

3、查询论坛的主题,并显示版块名称:

SELECT * FROM 数据库名.cdb_threads t, 数据库名.cdb_forums f WHERE t.fid = f.fid

4、如何调用版块简介

SELECT * FROM 数据库名.cdb_forums s,数据库名.cdb_forumfields f WHERE s.fid=f.fid

5、从SS取Discuz!含有图片附件的最新主题

SELECT * , a.attachment AS attachment FROM cdb_threads t, cdb_attachments a, cdb_posts p WHERE t.tid = a.tid AND a.isimage =1 AND p.tid = t.tid AND p.first =1 GROUP BY t.tid ORDER BY t.dateline DESC

6、读取论坛 某个用户所收藏的主题:

SELECT * FROM cdb_favorites f, cdb_members m, cdb_threads t WHERE f.uid = m.uid AND f.tid = t.tid

7、调用论坛中每个主题调用一张图片的SQL:

SELECT a. * , t. * , count( DISTINCT a.tid )
FROM cdb_attachments a, cdb_threads t
WHERE a.tid = (
SELECT t.tid
FROM cdb_threads t
WHERE t.attachment =2
ORDER BY dateline DESC )
GROUP BY a.tid

8、查询论坛的分类信息
论坛的分类信息是要四表联查的,并且不能一次查出来,分两次。第一次是将主题表与主题类型有联查(数据显示一次);第二次是查询分类信息的各个选项的值,查询出来后进行循环显示

SELECT * FROM cdb_threads t, cdb_threadtypes y WHERE t.sortid = y.typeid AND t.tid=4
SELECT * FROM cdb_typeoptionvars v,cdb_typeoptions o WHERE o.optionid=v.optionid AND v.tid=4 AND v.sortid=1

9、从SS取Discuz!含有图片附件的最新主题

SELECT * , a.attachment AS attachment FROM cdb_threads t, cdb_attachments a, cdb_posts p WHERE t.tid = a.tid AND a.isimage =1 AND p.tid = t.tid AND p.first =1 GROUP BY t.tid ORDER BY t.dateline DESC LIMIT 0,10

如有其他需要请跟帖

原作者:lidq.jingwu

相关文章:

1、SupeSite中用sql调用ecms文章方法
https://www.onexin.net/supesite-using-sql-method-calls-ecms-article/

2、SupeSite常用sql语句(一)
https://www.onexin.net/supesite-common-sql-statements/

3、创建 PDO 实例请求连接SQL数据库
https://www.onexin.net/pdo-database/

4、数据库设计中的英文术语表
https://www.onexin.net/english-glossary-database-design/

5、Magento中获取Zend DB对象中的SQL语句
https://www.onexin.net/magento-zend-db-object-for-sql-statements/

转载请注明出处:https://www.onexin.net/supesite-commonly-used-high-level-articles-sql-statement-b/

Leave a Reply

Your email address will not be published. Required fields are marked *