MYSQL多表联合查询的问题

WBOY
Libérer: 2016-06-06 20:45:28
original
1449 Les gens l'ont consulté

总共有三个表

post表:id,title,content
terms表:id,name
rela表:postid,termid

post表保存文章内容(文章ID、文章标题、文章内容),terms表保存分类内容(分类ID、分类名称),rela表保存的是文章分类信息,也就是文章ID对应的分类ID。

单纯匹配post表的title和content我知道是这样

<code>query( 
        "
        SELECT id, title
        FROM post
        WHERE (
            `title` LIKE  '%关键词%'
            OR  `content` LIKE  '%关键词%'
        )
        LIMIT 0 , 30
        "
    );
</code>
Copier après la connexion
Copier après la connexion

现在也要匹配相应的terms表的name,请问怎么写呢?

回复内容:

总共有三个表

post表:id,title,content
terms表:id,name
rela表:postid,termid

post表保存文章内容(文章ID、文章标题、文章内容),terms表保存分类内容(分类ID、分类名称),rela表保存的是文章分类信息,也就是文章ID对应的分类ID。

单纯匹配post表的title和content我知道是这样

<code>query( 
        "
        SELECT id, title
        FROM post
        WHERE (
            `title` LIKE  '%关键词%'
            OR  `content` LIKE  '%关键词%'
        )
        LIMIT 0 , 30
        "
    );
</code>
Copier après la connexion
Copier après la connexion

现在也要匹配相应的terms表的name,请问怎么写呢?

<code>select post.id, post.title from rela left join post on rela.postid = post.id left join terms on rela.termid = terms.id where post.title like '%关键词%' or post.content like '%关键词%' or terms.name like '%关键词%'
</code>
Copier après la connexion

联合查询是 Mysql 比较基础的操作,题主应该花点时间掌握这个基础知识点。:)

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!