织梦SQL查询Mysql数据库的几个例子

摘要:织梦SQL查询Mysql数据库的几个例子

例子1

  • 子查询:查询TAG标签tid=17的所有文章ID
  • 父查询:查询TAG标签tid=17的所有文章内容
SELECT body FROM dede_addonarticle
WHERE aid in(
  SELECT aid FROM dede_taglist WHERE tid=17
)

例子2

  • 子查询1:查询TAG标签tid=17的所有文章ID
  • 子查询2:查询TAG标签tid=17的所有文章ID
  • 父查询:查询TAG标签tid=17的所有文章标题
SELECT title FROM dede_archives
WHERE id in (
  SELECT aid FROM dede_addonarticle WHERE aid in (
    SELECT aid FROM dede_taglist WHERE tid=17
  )
)

例子3

  • 子查询:查询TAG标签tid=17的所有文章ID
  • 父查询:查询TAG标签tid=17的所有文章标题和内容
select a.title,b.body from dede_archives a
left join dede_addonarticle b
on a.id = b.aid
where b.aid in (
  SELECT aid FROM dede_taglist WHERE tid=17
)

例子4

查询栏目ID=1的随机8篇文章标题、缩略图和内容

select a.litpic,a.title,b.body from dede_archives a
left join dede_addonarticle b
on a.id = b.aid
where a.typeid=1
order by rand() limit 8

例子5

查询文章标题含有"ab"的文章标题、内容和文章ID

select a.title,b.body,c.id from dede_archives a
left join dede_addonarticle b
on a.id = b.aid
left join dede_arctiny c
on c.id = a.id
where a.title like '%ab%'

例子6

删除文章标题含有"ab"的所有内容

select a.title,b.body,c.id from dede_archives a
left join dede_addonarticle b
on a.id = b.aid
left join dede_arctiny c
on c.id = a.id
where a.title like '%ab%'
delete a,b,c

例子7

查询文章描述含有"%"的文章标题、内容和文章ID

select a.title,b.body,c.id from dede_archives a
left join dede_addonarticle b
on a.id = b.aid
left join dede_arctiny c
on c.id = a.id
where a.description like '%\%%'

注:转义字符 \ 表示后面的 % 为字符,不是通配符

版权声明:本文为博主原创文章,未经博主允许不得转载。http://www.dedenotes.com/course/dede-select.html
(1)
打赏 微信扫一扫 微信 支付宝 QQ 扫码打赏