我有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连接才有意义。
实现这一目标的最佳方法是什么?
答案 0 :(得分:0)
我认为需要union
,如下所示:
<?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
, a.artist_id as id
, 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
为1
,博客项0
为{{1}}。
如果您选择多位艺术家,则查询会对每位艺术家的结果进行排序。