如何解决博客和新闻交织在一起-加入还是联合或其他?
| 我有4张桌子。news: news_link: blog
--------- ----------- ---------
news_id,news_link_id,blog_id,news_title,news_link_news_id,blog_artist_id,news_content,news_link_artist_id blog_title,news_image,blog_content,news_date,blog_date,news_sticky_status,blog_date_edited,news_sticky_order,blog_status,news_status,blog_keywords
news_keywords,news_date_edited
另一张桌子是给艺术家的
我执行基本连接使用news_link将新闻项链接到艺术家时没有问题。
我的博客表中包含artist_id。
我想按日期在特定艺术家页面上显示博客条目,并使与该艺术家相关的新闻项出现在博客项目中。
这是我想要的输出:
title,date,content,status
我尝试了一些加入。我需要工会吗?这些表具有不同的标签,新闻表需要有news_link连接才有意义。
实现此目标的最佳方法是什么?
解决方法
我认为需要ѭ2,例如:
<?php
$artist = mysql_real_escape_string($_GET[\'artist\']);
$query =
\"SELECT is_news,title,date,content,status FROM (
SELECT
1 as is_news,a.artist_id as id,n.news_title as title,n.news_date as date,n.news_content as content,n.news_status as status
FROM artist a
INNER JOIN news_link nl ON (a.artist_id = nl.news_link_artist_id)
INNER JOIN news n ON (n.news_id = nl.news_link_news_id)
WHERE a.artist_id = \'$artist\'
UNION
SELECT
0 as is_news,b.blog_title as title,b.blog_date as date,b.blog_content as content,b.blog_status as status
FROM artist a
INNER JOIN blog b ON (a.artist_id = b.blog_artist_id)
WHERE a.artist_id = \'$artist\'
) s
ORDER BY s.id,s.date\";
新闻项的is_news
为ѭ5,博客项的0
。
如果您选择多个艺术家,则查询将对每个艺术家的结果进行排序。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。