加入三(3)个MySQL表(Join three (3) MySQL tables)

编程入门 行业动态 更新时间:2024-10-08 10:52:57
加入三(3)个MySQL表(Join three (3) MySQL tables)

不幸的是我不像SQL那么好,而且我试图在三个表之间完成连接。

这是一个粗略的简化表结构:

links: id, url, description categories: id, name, path link_cat: link_id, cat_id

我瞄准的选择语句应该有

links.id, link.url, link.description, categories.name, categories.path

通过link_cat表匹配链接和类别的位置。 我认为只要每个链接只有一个类别,就不应该太难。 这就是我所假设的。 如果不是,那么将另一种将多个类别逗号分隔为categories.name字段的方法会更好。

我希望这一切都是可以理解的,听起来并不太愚蠢。

unfortunately i'm not that good as SQL and i'm trying to get a join between three tables done.

here's a rough simplified table structure:

links: id, url, description categories: id, name, path link_cat: link_id, cat_id

The select statement I'm aiming for should have

links.id, link.url, link.description, categories.name, categories.path

Where links and categories are matched via the link_cat table. I think that shouldn't be too hard as long as there's only one category for each link. This is what I'm assuming. If not it would be good to have another way that pulls multiple categories comma separated into the categories.name field.

I hope this is all understandable and doesn't sound too silly.

最满意答案

SELECT links.id, links.url, links.description, categories.name, categories.path FROM links INNER JOIN link_cat ON links.id = link_cat.links_id INNER JOIN categories ON categories.id = link_cat.category_id SELECT links.id, links.url, links.description, categories.name, categories.path FROM links INNER JOIN link_cat ON links.id = link_cat.links_id INNER JOIN categories ON categories.id = link_cat.category_id

更多推荐

本文发布于:2023-08-04 19:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MySQL   Join   tables

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!