Home > Backend Development > PHP Tutorial > 求一条三表关联查询的MySQL语句

求一条三表关联查询的MySQL语句

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:52:42
Original
1232 people have browsed it

各位,求一条SQL语句,我自己弄了半天弄出来的总是不对,结果都有重复项,麻烦帮我看下谢谢啊。

三个表,content、relationship、meta,

content是内容,主键cid;
meta表存储分类的相应名称、类型等,主键mid;
relationship是content和meta之间的关系,两个键,cid和对应的mid。

求个SQL就是给一个分类名称,比方说default,查出相关的content内容和meta的内容放入数组。
PS.有点像Wordpress里面的数据库结构,找了半天没找到WP是怎么实现的……弱啊

我试着写了几个,但是都有点不对,JOIN没学好,麻烦大家帮助下,谢谢。

回复内容:

各位,求一条SQL语句,我自己弄了半天弄出来的总是不对,结果都有重复项,麻烦帮我看下谢谢啊。

三个表,content、relationship、meta,

content是内容,主键cid;
meta表存储分类的相应名称、类型等,主键mid;
relationship是content和meta之间的关系,两个键,cid和对应的mid。

求个SQL就是给一个分类名称,比方说default,查出相关的content内容和meta的内容放入数组。
PS.有点像Wordpress里面的数据库结构,找了半天没找到WP是怎么实现的……弱啊

我试着写了几个,但是都有点不对,JOIN没学好,麻烦大家帮助下,谢谢。

搞定了。

SELECT * 
FROM hycms_metas AS m
RIGHT JOIN hycms_relationships AS r ON m.mid = r.mid
JOIN hycms_contents AS c ON r.cid = c.cid
WHERE m.slug =  'default'
AND m.mid IS NOT NULL
Copy after login
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template